开发者

How can I assigned onclick for a tag with jquery

Does anyone know how can I assigned onclick for a tag with jquery

Exmaple:

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

I want to assign onclick开发者_Python百科="test()" in the above a tag by jquery, anyone know how to do it?


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

Source: http://docs.jquery.com/Events/click

For your update request, as ecounysis said, you can bind the function, bu make sure it returns false, to prevent the real link from working (if you have a href):

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

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

if you can't change test, you can also do:

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

Example: http://jsbin.com/aseku


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜