如何为jquery的标签分配onclick

时间:2009-12-17 05:10:30

标签: jquery

有谁知道如何为jquery

的标签分配onclick

〔实施例:

<a id="abc">abc</a>

我想通过jquery在上面的标签中分配onclick =“test()”,有谁知道怎么做?

2 个答案:

答案 0 :(得分:4)

$(document).ready(function(){ // all jQuery code should run from this event
    $('div').click(function(){
        // find the div that was clicked on:
        $(this).css('color', 'red');
    }); //click
}); //doc.ready

来源:http://docs.jquery.com/Events/click

对于您的更新请求,正如ecounysis所说,您可以绑定该函数,确保它返回false,以防止真正的链接起作用(如果您有href):

function test(){
  alert("Hello");
  return false;
}

$(function(){
  $('#abc').click(test);
});

如果您无法更改test,您还可以执行以下操作:

$('#abc').click(function(){
      test();
      return false;
});

示例:http://jsbin.com/aseku

答案 1 :(得分:2)

$(function() { 
    $("#abc").click(test);
});