2013-02-13 82 views
0

我想选择基于其内容的元素选择元素,但我似乎无法得到它的工作。似乎不是基于内容

我一直在使用与match()正则表达式的尝试:

$('.mod') 
    .filter(function() { 
    return this.match(/abc/); 
}) 
    .html("Test Worked!") 
; 

而且.text()

$('.mod') 
    .filter(function() { 
    return this.text() == 'abc' ; 
}) 
    .html("Test Worked") 
; 

但既不工作。

我做错了什么?我宁愿使用正则表达式,但现在我只想以某种方式选择元素。

小提琴这里:http://jsfiddle.net/sethj/z3MBw/2/

+0

'this'指元素节点。它没有匹配方法或文本方法。 – 2013-02-13 19:02:43

回答

4

文本()是一个jQuery方法:

$('div').filter(function() { 
    return $(this).text().match(/mod/); 
}).html("Matched!"); 

FIDDLE

3

使用此:

$('.mod') 
    .filter(function() { 
    return $(this).text() == 'abc' ; 
})... 

不能在DOM元素上调用text(),你必须包裹元素作为一个jQuery对象。