简体   繁体   English

JQuery变量脚本

[英]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. 我如何制作它以便我可以使用变量哈希,而不是输入hash_tag_input的内容作为搜索词。 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 . 对于第1部分,将$('#hash_tag_input').val()替换为hash
For part 2, just put the getJSON call directly in $(document).ready , like this: 对于第2部分,只需将getJSON调用直接放在$(document).ready ,如下所示:

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>');              
         }
    });  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM