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/

4

2 回答 2

4

text() 是一个 jQuery 方法:

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

小提琴

于 2013-02-13T19:02:19.017 回答
3

用这个 :

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

您不能调用text()DOM 元素,必须将元素包装为 jQuery 对象。

于 2013-02-13T19:02:00.183 回答