JQuery Variable Script
开发者_如何学运维I have the code:
<script type="text/javascript" charset="utf-8">
var hash = "fifa";
$(document).ready(function() {
console.log("I am ready");
$('#trigger_but').click(function() {
console.log("Click Performed");
$.getJSON("http://search.twitter.com/search.json?rpp=100&callback=?&q=%23" + $('#hash_tag_input').val(), function(data) {
for (var i = 0; i < data.results.length; i++) {
$('#result').prepend('<div class="tweet"><img src="' + data.results[i].profile_image_url
+ '" width="50" height="60"/><span id="tweetText">' + data.results[i].text + '</span></div>');
}
});
});
});
</script>
</head>
<body>
<input type="text" id="hash_tag_input" size="40"/>
<input type="button" id="trigger_but" value="Fetch Tweets"/>
<div id="result"></div>
</body>
How would I make it so that I can use the variable hash, instead of the inputted contents of hash_tag_input as the search term. And make it so that it fetches the tweets automatically without the click of a button.
For part 1, replace $('#hash_tag_input').val()
with hash
.
For part 2, just put the getJSON call directly in $(document).ready
, like this:
var hash = "fifa";
$(document).ready(function(){
$.getJSON("http://search.twitter.com/search.json?rpp=100&callback=?&q=%23"+hash,function(data){
for(var i=0;i<data.results.length;i++){
$('#result').prepend('<div class="tweet"><img src="'+data.results[i].profile_image_url + '" width="50" height="60"/><span id="tweetText">'+data.results[i].text+'</span></div>');
}
});
});
精彩评论