admin管理员组文章数量:1418964
I have some <TD>s
without unique names. Inside them there are spans with unique classnames so I have no problem targetting the spans. How can I target the parent <td>
so I can change its class?
I want to do something like $(".classname").parent("TD").className="newClassclassname"
.
I have some <TD>s
without unique names. Inside them there are spans with unique classnames so I have no problem targetting the spans. How can I target the parent <td>
so I can change its class?
I want to do something like $(".classname").parent("TD").className="newClassclassname"
.
-
1
You can use
.find('.newClass');
– Khez Commented Apr 19, 2011 at 14:31
5 Answers
Reset to default 6You were close:
$('.classname').parent('td').addClass('newClassName');
Though typically it's safer to go with:
$('.classname').closest('td').addClass('newClassName');
... which doesn't assume the <td>
is the immediate parent.
The reason .className
doesn't work is because jQuery returns elements wrapped in the jQuery object. If you want to access the original (DOM) object you need to select the first item in the jQuery collection with [0]
:
$('.classname').parent('td')[0].className = 'newClassName';
But I remend using the jQuery function addClass()
anyway since it won't interfere with existing classes.
You can do
$("span.classname").closest("td").addClass("newClassclassname");
Sorry, do you mean following code:
$(".classname").parent("TD").addClass("newClassName");
$(".classname").parent("TD")[0].className="newClassname";
$(".classname").parent().addClass("newClassclassname");
本文标签: javascriptHow can I target an element39s 39parent39Stack Overflow
版权声明:本文标题:javascript - How can I target an element's 'parent' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745299634a2652292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论