JQuery变量脚本

JQuery Variable Script

本文关键字:脚本 变量 JQuery      更新时间:2023-09-26

我有代码:

<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>

如何使用变量哈希,而不是使用hash_tag_input的输入内容作为搜索项?并使其自动获取tweet而无需点击按钮。

对于第一部分,将$('#hash_tag_input').val()替换为hash
对于第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>');              
         }
    });  
});