[英]How to Use jQuery Selector to Select Table Row Containing a Specific Link
I'm conditionally styling a table based on certain elements it contains. 我有条件地根据其中包含的某些元素来设计表格的样式。 For example, the following code adds a class based on whether the tr contains the word 'Novel':
例如,以下代码根据tr是否包含单词“ Novel”添加一个类:
<script>$( "tr:contains('Novel')" ).addClass('row-highlight-novel');</script>
But what I'd like to do is the same thing, but with a link, where I could add a class to the tr based on whether it has a specific url or not. 但是,我想做的是同一件事,但是有一个链接,我可以根据它是否具有特定的URL向tr添加一个类。
I've tried $("a[href='http://www.link-here.org/']")
, but that only selects the link, not the table row it's in. I can't seem to find the right selector that will do this for the whole table row. 我试过
$("a[href='http://www.link-here.org/']")
,但是那只选择链接,而不是它所在的表行。我似乎找不到正确的选择器,将对整个表格行执行此操作。 Your help would be much appreciated! 您的帮助将不胜感激!
Without further context I would think using .closest()
would work: 没有更多的上下文,我认为使用
.closest()
可以工作:
$("a[href='http://www.link-here.org/']").closest('tr')
Ref: https://api.jquery.com/closest/ 参考: https : //api.jquery.com/closest/
This is how i would do it 这就是我会做的
html html
<a href="http://test.com/?foo=bar&baz=quux"><tr>hello</tr></a>
<a href="http://example.ca/?foo=bar&baz=quux"><tr>hello</tr></a>
Jquery selector jQuery选择器
$('a[href*="test"]').each(function() {
$(this).addClass('test');
});
here is complete fiddle http://jsfiddle.net/q0nc3dzq/3/ 这是完整的小提琴http://jsfiddle.net/q0nc3dzq/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.