简体   繁体   English

如何使用jQuery选择器选择包含特定链接的表行

[英]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.

相关问题 如何使用jquery遍历php回显的表行并选择满足属性的特定行 - How to use jquery to loop through table rows echoed by php and select a specific row where a property is meet 如何使用JQuery第一个选择器进行特定选择 - How to use JQuery first selector for specific selection jQuery:查找包含两个具有特定文本的单元格的表行 - jQuery : find table row containing two cells that have specific text 如何使用链接的行ID和rel属性在特定表行内容上指向onclick事件-jQuery - How to point onclick event on a specific table row content using the id of the row and rel attribute of link - jQuery 包含表选择器问题的Jquery复选框 - Jquery checkboxes within containing table selector question 如何使用 jQuery :contains 选择器选择具有特定文本的中间节点? - How to select middle nodes with specific text using jQuery :contains selector? jQuery选择器存储在变量中:如何通过属性值选择特定元素? - jQuery selector stored in variable: How to Select specific elements by attribute value? 如何使用jQuery选择器和JavaScript选择特定的HTML节点? - How to select a specific html node with a jquery selector and JavaScript? 如何使用jQuery链接到表中的特定锚点 - How to link to specific anchor in table with jquery 使用单选择器从表中选择链接 - Select link from table using single selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM