jQuery text match
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."
精彩评论