简体   繁体   English

javascript变量上的jQuery选择器

[英]jquery selector on javascript variable

Hey guys I'm having trouble implementing some jQuery code. 大家好,我在实现一些jQuery代码时遇到了麻烦。 I have a javascript function that stores the element that the function was called from (a td) and then selects it's parent node and changes the class of that parentnode (a tr). 我有一个javascript函数,该函数存储从(td)调用该函数的元素,然后选择它的父节点并更改该父节点的类(tr)。 I would like to change it to jQuery so I can take ad vantage of the .addClass tool. 我想将其更改为jQuery,以便可以利用.addClass工具的优势。 Here is the code 这是代码

   function draftHandler(teamID, element, from, name, pos)
{
     if(...something...) 
          element.parentNode.className = "drafted";
}

I have been trying something like... 我一直在尝试类似...

element.closest('tr').addClass('drafted');

...without any luck. ...没有任何运气。 Thanks in advance! 提前致谢!

$(element).closest('tr').addClass('drafted');

尝试:

  $(element).parent().addClass('drafted');
$('td').parent('tr').addClass('drafted').

You can replace $('td') with the element. 您可以将$('td')替换为元素。

The parent('tr') is only added security making sure you indeed add the class to the parent tr element. 仅增加安全性的parent('tr')可以确保您确实将类添加到了父tr元素。 It's not needed IF the first parent of your td is in fact a tr . 如果您的td的第一父级实际上是tr则不需要。

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

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