开发者

Find html text and tag parent

开发者 https://www.devze.com 2023-03-05 01:30 出处:网络
There is such a structure <tr class=\"\"> ... <span class=\"\">my-marker-1</span> ... </tr>

There is such a structure

<tr class="">
   ...               
                        <span class="">my-marker-1</span>
   ...
 </tr>

How to find only word "marker" and give the tr a special开发者_Go百科 class?


You could use the :contains() selector:

$("tr:contains(my-marker-1)").addClass("my-class-1");

However, you should be aware that :contains() performs a wildcard search on the text of an element, so it would match cat in category or pit in capital. If that's too broad for you, you can use filter():

$("tr").filter(function () { 
    return $(this).find("span").text() == "my-marker-1";
}).addClass("my-class-1");


Try something like this.

$("tr td span.label-field").val("marker").closest("tr").addClass("special");


If "marker" is always going to be in in a span:

$("span").each(function() {
    if ($(this).text() == "my-marker-1") {
        $(this).parent("tr").addClass("special_class");
    }
});


Assuming you want to apply a class to all TRs with the class tabledrag-leaf that contain spans with text containing the word "marker", this will do what you need:

$('tr.tabledrag-leaf td').filter( function () {
    return $(this).children('span.label-field').html().indexOf('marker') !== -1;
}).parent().addClass('has-marker');

See jQuery.filter() for details on how this works.

0

精彩评论

暂无评论...
验证码 换一张
取 消