Jquery 导航到父级。使用parent()或closest()选择器的问题

发布于 2024-12-15 23:42:17 字数 1139 浏览 4 评论 0原文

我无法实现看起来如此简单的事情。不知道我哪里出错了。请指教。 目标是突出显示表行是否选中了与其对应的复选框。我已经提到了几个关于使用 .closest('tr') 或 .parent('tr') 来实现此功能的示例,但在我的情况下出于某些原因 - 它超出了实现范围,即它突出显示了所有行而不仅仅是行被选中的一个。

我的 HTML

<tr class="EvenRowClass">
 <td class="ColClass">
   <div class="wrapClass" ">
 <span class="tableCol" style="background: someimage.png") </span>
 <span class="tableCol" style="background: someimage.png") </span>
 <input id="Check1" class="selectCheck" type="checkbox"
      onclick="highlightRow(this,Check1); return false;" value="Check1">
</div>
 </td>
 <td>SomeCode</td>
 <td>SomeCode</td>
 <td>SomeCode</td>

假设还有另一个这样的代码块,每个复选框都有一个唯一的 ID(顺序))

单击时调用的 JavaScript 函数

    function highlightRow(checkbox, id)
{
   $("tr:parent").toggleClass("highlight", checkbox.checked);   
}   

上面的 javascript 选择表中的所有内容和页面的其余部分,无论它们是否被选中并向其应用突出显示类。 我使用复选框选择器、.closest('tr')、.parent('tr') 尝试了其他几个版本,但没有一个对我有用。

最后提到的几个只是在某种意义上更好,它们仅影响表内的内容而不影响页面上的其他内容。

请指教。我已经为此挠头了,现在开始疼了。

I am not able to achieve something that seems so simple. Not sure where I am going wrong. Please advise.
Goal is to highlight a table row is the checkbox corresponding to it is selected. I have referred to several examples that talks about using .closest('tr') or .parent('tr') to get this working but for some reasons in my case - its over achieving i.e. it highlights all the rows rather than just the one that is selected.

My HTML

<tr class="EvenRowClass">
 <td class="ColClass">
   <div class="wrapClass" ">
 <span class="tableCol" style="background: someimage.png") </span>
 <span class="tableCol" style="background: someimage.png") </span>
 <input id="Check1" class="selectCheck" type="checkbox"
      onclick="highlightRow(this,Check1); return false;" value="Check1">
</div>
 </td>
 <td>SomeCode</td>
 <td>SomeCode</td>
 <td>SomeCode</td>

Assume that there is another block of such code with a and unique ids for each check box sequential)

My JavaScript function that gets called on click

    function highlightRow(checkbox, id)
{
   $("tr:parent").toggleClass("highlight", checkbox.checked);   
}   

The javascript above selects all the in the table and rest of the page regardless of whether they are checkd or not and applies the highlight class to it.
I tried several other version using checkbox selector , .closest('tr'), .parent('tr') but none of them have worked for me.

The last few mentioned are only better in the sense they affect the only within the table and not others on the page.

Please advise. I have scratched my head over this enough that it started hurting now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2024-12-22 23:42:17
function highlightRow( checkbox, id ) {
    $( checkbox ).closest( 'tr' ).toggleClass( 'highlight', checkbox.checked );  
};
function highlightRow( checkbox, id ) {
    $( checkbox ).closest( 'tr' ).toggleClass( 'highlight', checkbox.checked );  
};
鲜肉鲜肉永远不皱 2024-12-22 23:42:17

试试这个:

   function highlightRow(checkbox, id)
{
   $(checkbox).parents("tr").toggleClass("highlight", checkbox.checked);   
}   

Try this :

   function highlightRow(checkbox, id)
{
   $(checkbox).parents("tr").toggleClass("highlight", checkbox.checked);   
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文