jQuery文字比对 [英] jQuery text match

查看:203
本文介绍了jQuery文字比对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本的锚标记,我想检查给定的var是否与字符串完全匹配.

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

这可行,但是我想使用contains以外的东西,因为如果包含给定的字符串,它将匹配两个元素.我希望它完全匹配.

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.

有什么想法吗?

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

推荐答案

您可以使用.filter()函数而不是:contains过滤器将自定义过滤器应用于选择器结果集.

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');

这篇关于jQuery文字比对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆