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

如果您添加一个 id 或一个类,您可以添加

   $('.yourClass').click(function()
    {
        var x = $(this).attr('href');
        on_search();
        // you would probably want to pass the item name as a parameter: on_search(x);
    });
于 2013-06-08T14:43:17.560 回答
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 回答