第1行:
line 1:
<input id="Text1" type="text" /><input id="Button1" type="button" value="button" /><br />第2行:
line 2:
<a target="_blank" href="search.twitter/search?q=#msdhoni">#msdhoni</a>
我想要什么 - 当用户在文本框中输入一些东西(在第1行),该东西应该成为div的标题(在第2行),点击botton(在第1行)。实际上,我们希望用代码中的#msdhoni代替文本框中的文本输入。
What I want- when a user input some thing in text box (in line 1) that should become the title of the div (in line 2) on click of the botton (in line 1). Actually want to replace #msdhoni everywhere in code with the text input in the textbox.
使用Jquery代码:(请在此代码中建议更改以使其成为可能)
Jquery code I m using:(Please suggest the change in this code to make this possible)
String.prototype.linkify=function(){ return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&;\?\/.=]+/g,function(m){ return m.link(m); }); }; String.prototype.linkuser=function(){ return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u){ return u.link("twitter/"+u.replace("@","")); }); }; String.prototype.linktag=function(){ return this.replace(/[]+[A-Za-z0-9-_]+/,function(t){ return t; }); }; var showTweetLinks='none'; function fetch_tweets(elem){ elem = $(elem); keyword = escape(elem.attr('title')); num=elem.attr('class').split(' ').slice(-1); var url = "search.twitter/search.json?q=" +keyword+ "&rpp=" + num + "&callback=?"; $.getJSON(url,function(json){ $(json.results).each(function(){ var tTime=new Date(Date.parse(this.created_at)); var cTime=new Date(); var sinceMin=Math.round((cTime-tTime)/60000); if(sinceMin==0){ var sinceSec=Math.round((cTime-tTime)/1000); if(sinceSec<10) var since='less than 10 seconds ago'; else if(sinceSec<20) var since='less than 20 seconds ago'; else var since='half a minute ago'; } else if(sinceMin==1){ var sinceSec=Math.round((cTime-tTime)/1000); if(sinceSec==30) var since='half a minute ago'; else if(sinceSec<60) var since='less than a minute ago'; else var since='1 minute ago'; } else if(sinceMin<45) var since=sinceMin+' minutes ago'; else if(sinceMin>44&&sinceMin<60) var since='about 1 hour ago'; else if(sinceMin<1440){ var sinceHr=Math.round(sinceMin/60); if(sinceHr==1) var since='about 1 hour ago'; else var since='about '+sinceHr+' hours ago'; } else if(sinceMin>1439&&sinceMin<2880) var since='1 day ago'; else{ var sinceDay=Math.round(sinceMin/1440); var since=sinceDay+' days ago'; } var tweetBy='<a class="tweet-user" target="_blank" href="twitter/'+this.from_user+'">@'+this.from_user+'</a> <span class="tweet-time">'+since+'</span>'; if(showTweetLinks.indexOf('reply')!=-1) tweetBy=tweetBy+' · <a class="tweet-reply" target="_blank" href="twitter/?status=@'+this.from_user+' &in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">Reply</a>'; if(showTweetLinks.indexOf('view')!=-1) tweetBy=tweetBy+' · <a class="tweet-view" target="_blank" href="twitter/'+this.from_user+'/statuses/'+this.id+'">View Tweet</a>'; if(showTweetLinks.indexOf('rt')!=-1) tweetBy=tweetBy+' · <a class="tweet-rt" target="_blank" href="twitter/?status=RT @'+this.from_user+' '+escape(this.text.replace(/"/g,'"'))+'&in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">RT</a>'; var tweet='<div class="tweet"><div class="tweet-left"><a target="_blank" href="twitter/'+this.from_user+'"><img width="48" height="48" alt="'+this.from_user+' on Twitter" src="'+this.profile_image_url+'" /></a></div><div class="tweet-right"><p class="text">'+this.text.linkify().linkuser().linktag().replace(/<a/g,'<a target="_blank"')+'<br />'+tweetBy+'</p></div><br style="clear: both;" /></div>'; elem.append(tweet); }); }); return(false); } $(function(){ showTweetLinks=showTweetLinks.toLowerCase(); if(showTweetLinks.indexOf('all')!=-1) showTweetLinks='reply,view,rt'; $('.twitStream').each(function(){ fetch_tweets(this); }); });推荐答案
你的问题到处都是 - 问题/ HTML / JavaScript不匹配....以下是更新 href 和锚点的文本(不是 div )点击一个按钮
Your question is all over the place - the question / HTML / JavaScript dont match up .... below is a way to update the href and text of an anchor (not a div) on the click of a button
<input id="Text1" type="text" /><input id="Button1" type="button" value="button" /><br /> <a id="Link1" target="_blank" href="search.twitter/search?q=#msdhoni">#msdhoni</a>JavaScript:
JavaScript :
$('#Button1').click(function() { $('#Link1').attr('href',"search.twitter/search?q=#" + $('#Text1').val()); $('#Link1').text("#" + $('#Text1').val()); });注意:您需要添加 ID 到锚点以使这个代码工作
Note: You need to add an ID to the anchor to make this code work
更多推荐
将html文本框的值分配给div的标题
发布评论