使用jquery查找文本

时间:2013-05-28 04:27:24

标签: javascript jquery

我正在尝试使用jQuery找到一个元素并隐藏它。

有人能看出为什么这不起作用吗?

HTML:

 <span class='a'>2</span>

jQuery的:

 var abc = '2';

 $(".a:contains(abc)").hide();

2 个答案:

答案 0 :(得分:3)

试试这个

$(".a:contains(" + abc + ")").hide();

答案 1 :(得分:1)

您也可以使用filter

var abc = '2';

$('.a').filter(function() {
   return ( $(this).text() === abc ); //use this if you want exact match with the content
})
.hide();

$('.a').filter(function() {
   return ( $(this).text().indexOf(abc) != -1 ); //use this if you want contains match with the content.
})
.hide();