开发者

Adding a function to onclick event by Javascript!

Is it possible to add a onclick event to any button by jquery or something like we add class?

function onload()
{

//a开发者_运维问答dd a something() function to button by id

}


Calling your function something binding the click event on the element with a ID

$('#id').click(function(e) {
    something();
});

$('#id').click(something);

$('#id').bind("click", function(e) { something(); });

Live has a slightly difference, it will bind the event for any elements added, but since you are using the ID it probably wont happen, unless you remove the element from the DOM and add back later on (with the same ID).

$('#id').live("click", function(e) { something(); });

Not sure if this one works in any case, it adds the attribute onclick on your element: (I never use it)

$('#id').attr("onclick", "something()");

Documentation

  • Click
  • Bind
  • Live
  • Attr


Yes. You could write it like this:

$(document).ready(function() {
  $(".button").click(function(){
    // do something when clicked
  });
});


$('#id').click(function() {
    // do stuff
});


Yes. Something like the following should work.

$('#button_id').click(function() {
  // do stuff
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜