简体   繁体   English

jQuery addClass停止工作

[英]jQuery addClass stopped working

For some reason jQuery.addClass has stoped working. 由于某种原因,jQuery.addClass已停止工作。 I have no idea why. 我不知道为什么。 Other stuff works though. 其他东西虽然有效。

  // Change row background color on click
  jQuery('#rowList tr').live("click", function() {
    alert(jQuery(this).attr('title')); // Just here for testing. This works
    alert(jQuery(this).css('border','solid 1px red')); // Just here for testing. This works

    jQuery(this).closest("tr").siblings().removeClass("selected"); 
    jQuery(this).addClass("selected");  // NOT working
  });

Any reason why this is not working? 有什么原因不起作用? Here is my HTML: 这是我的HTML:

<table id="rowList">
  <tbody>
    <tr title="My title 1" class="imageItem odd"> <td>some stuff</td></tr>
    <tr title="My title 2" class="imageItem even"><td> some stuff here</td></tr>
  </tbody>
</table>

You have a missing ) on the second alert: 您在第二个警报中缺少)

alert(jQuery(this).css('border','solid 1px red'));
                                               ^

Once you fix that it works, you can test it here . 修复该功能后, 您可以在此处进行测试 As an aside, since you're on a <tr> , there's no need for the .closest("tr") call, you can remove it from the chain, something like this overall: .closest("tr") ,由于您使用的是<tr> ,因此不需要.closest("tr")调用,因此可以将其从链中删除,总的来说像这样:

CSS: CSS:

.bordered { border: solid 1px red; }

Script: 脚本:

jQuery('#rowList tr').live("click", function() {
  alert(jQuery(this).attr('title'));
  jQuery(this).addClass("bordered selected")
              .siblings().removeClass("selected"); 
});​

You can give it a go here . 您可以在这里试一试

Because the way you've written it is the .addClass() adds the class to the td not the parent tr . 因为您编写的方式是.addClass()将类添加到td而不是父tr

Unless the omission of closest('tr') was a typo? 除非省略closest('tr')是错字?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM