0

テキスト (item.name) をクリックして、この *on_search* 関数を呼び出すにはどうすればよいですか?

$.each(docs, function(i, item) {
            $('#body').append($('<div><a href="">' + item.name </a></div><br/>'));
       });

// call this function whenever user clicks the text
function on_search() {            
     var url ='http:myhomepage/select/?wt=json&json.wrf=?&q='+item.name;
     $.getJSON(url, on_text);
    }
4

3 に答える 3

0

イベントハンドラにクラスセットを付与してください。

   $.each(docs, function(i, item) {
                $('#body').append($('<div><a href="" class="my-anchor">' + item.name </a></div><br/>'));
    $(".my-anchor").click(on_search);
           });

    // call this function whenever user clicks the text
    function on_search() {            
         var url ='http:myhomepage/select/?wt=json&json.wrf=?&q='+item.name;
         $.getJSON(url, on_text);
        }
于 2013-06-08T14:37:53.597 に答える
0

各リンクを独自の div 内にラップする必要があるかどうかは不明です。実際、これをうまく機能させるには、これらすべてのリンクを特定の div 内に配置することをお勧めします。

そこから、次のことを行います。

$('#divName').find('a').click(function (event) {
    var url=...
    event.preventDefault(); // Stops the browser from actually following the link
    [...]
});
于 2013-06-08T14:38:40.907 に答える