admin管理员组

文章数量:1323348

I am new to jQuery and cannot get a selector to work using the id of the element. The code below works:

$(function() {  
  /* $("#ShowBox").onclick = ShowAccessible;  */  
  document.getElementById("ShowBox").onclick = ShowAccessible;  
  /* $(".searchcheck").click = ShowAccessible; */  
});

function ShowAccessible() {  
  $("tr.hide").fadeOut();  
}

However, neither of the two mented out lines work i.e. they do not apply the click event to a checkbox named "ShowBox" with a class of "searchcheck". Why is this?

I am new to jQuery and cannot get a selector to work using the id of the element. The code below works:

$(function() {  
  /* $("#ShowBox").onclick = ShowAccessible;  */  
  document.getElementById("ShowBox").onclick = ShowAccessible;  
  /* $(".searchcheck").click = ShowAccessible; */  
});

function ShowAccessible() {  
  $("tr.hide").fadeOut();  
}

However, neither of the two mented out lines work i.e. they do not apply the click event to a checkbox named "ShowBox" with a class of "searchcheck". Why is this?

Share Improve this question edited May 22, 2009 at 12:52 Tomalak 338k68 gold badges546 silver badges635 bronze badges asked May 22, 2009 at 11:35 yubenyuben 6931 gold badge9 silver badges23 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

there is no onclick property for a jQuery object; when you use $([selector]), a jQuery object is returned, not an element.

Use either

.click(function () { ...}); // can be an anonymous function or named function

or

.bind('click', function() { ... }); // can be an anonymous function or named function

like so

$("#ShowBox").click(ShowAccessible);
$("#ShowBox").bind('click', ShowAccessible);

JQuery uses functions rather than properties to set the handlers.

$("#ShowBox").click(ShowAccessible); 

if you really had to do it with an assignment , i think

$("#ShowBox")[0].onclick = ShowAccessible;

would work.

本文标签: javascriptHow does jQuery handle the click eventStack Overflow