0

Twitter の検索結果のスクロール リストを持つページを作成しようとしています (jQuery の twitter.search を使用)。ユーザーはボタンをクリックして検索語を変更できる必要があります (したがって、「Apple」をクリックすると検索語が「Apple」に変更されます。

変数を使用して検索語を設定しています。その変数をテキスト文字列 (つまり、searchText = "Pineapple";) に設定すると機能します。しかし、ボタンを使用して変数を変更しようとしても、何も起こりません。

//編集 - ボタンは変数を変更していないようです。考え?ありがとう。

<script type="text/javascript">
//set default value for searchText
    searchText = "Pineapple";

//this sets up our Twitter stream
    $(document).ready(function() {

        $('#twitter-stream').twitterSearch({ 
        term: searchText, 
        // no fade 
        animOut: { opacity: 1 }, 
        avatar:  false, 
        anchors: false, 
        bird:    false, 
        colorExterior: 'white', 
        colorInterior: 'white', 
        pause:   true, 
        time:    false, 
        timeout: 2000 
        });
    });
</script>

<script type="text/javascript">
    $("#button-container").on("button", "click", function( e ) {
        searchText = $(this).text();
    });
</script>

<div id="twitter-stream" title="Mouse away to un-pause">
</div>

<div id="button-container">
<button class="button"> Apple </button>
</div>

<button onclick="alert(searchText);">Click me!
</button>
4

1 に答える 1

5

このコードは非常に非効率的です。ほぼ同じ 4 つの関数とは対照的に、jQuery を使用して、ボタンのすべてのクリック イベントを処理する 1 つの関数を定義できます。

jQuery の を使用してイベントを委任します.on

$("#someContainerDiv").on("button", "click", function( e ) {
    searchText = $(this).text();
});

ボタン自体の innerText を取得するだけなので、値の配列が不要になります。

参考文献

これはjQueryに関する情報であり、これが置き換える機能onに関する情報です。delegate

于 2012-07-30T19:47:57.603 に答える