开发者

jQuery text match

开发者 https://www.devze.com 2022-12-13 15:05 出处:网络
I have an anchor tag with text and I want to check if the given var matches the string exactly. This works, but I would like to use something other than co开发者_StackOverflow中文版ntains, since it w

I have an anchor tag with text and I want to check if the given var matches the string exactly.

This works, but I would like to use something other than co开发者_StackOverflow中文版ntains, since it will match two elements if the contain the given string. I want it to match exactly.

Any ideas ?

function test(submenu){
$('a:contains("' + submenu + '")', 'ul.subMenu li').css('font-weight', 'bold');
}


You can use the .filter() function instead of the :contains filter to apply a custom filter to your selector result set.

In this case apply a custom filter to look for text with an exact match. Something like:

$('a').filter( function (index) {
    return $(this).text() == submenu;
})
  .css('font-weight', 'bold');


Is this what you're looking for:

function test(submenu)
{
   if ($("ul.submenu li a").text() == submenu)
   { 
      // do whatever here 
   }
}

However, I'm not sure what you mean by, "This works, but I would like to use something other than contains, since it will match two elements if the contain the given string. I want it to match exactly."

0

精彩评论

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

关注公众号