0

我正在做这样的事情来从类的子节点中检索文本内容:

$(data).find(".mw-content-ltr").contents().each(function(_, row) {
    // ...
    } else if ( row.nodeType === 3 && $.trim(row.nodeValue).length ) {
       var textNodeValue = $.trim(row.nodeValue);
       console.log(textNodeValue);
    }
});

问题是如果文本节点内部有一些锚点,像这样:

When<a href="/wiki/Raquel_Ochmonek" title="Raquel Ochmonek"> Raquel Ochmonek</a> comes over to sit 
for <a href="/wiki/Brian_Tanner" title="Brian Tanner">Brian</a>, 
a burglar breaks into the house through the master bedroom 
where <a href="/wiki/ALF" title="ALF">ALF</a> is hiding. 

我的代码不会检索它们,导致如下所示:

When

comes over to sit for

, a burglar breaks into the house through the master bedroom where

is hiding. 

当我需要这样的东西时:

When  Raquel Ochmonek  comes over to sit for Brian, a burglar breaks into the house through the master bedroom where is hiding. 

提前致谢!

4

1 回答 1

3

尝试这个:

$(".mw-content-ltr").text()

小提琴比较两者:http: //jsfiddle.net/KyleMuir/GeRVV/2/

编辑:由于类中有其他你不感兴趣的数据,这样的东西会起作用吗?

$(".mw-content-ltr").contents().each(function (_, row) {
    if (row.nodeType === 3 && $.trim(row.nodeValue).length) {
        var textNodeValue = $.trim(row.nodeValue);
        console.log(textNodeValue);
    } else if (row.nodeType == 1) {
        var nodeValue = $.trim($(row).text());
        console.log(nodeValue);
    }
});

JSFiddle:http: //jsfiddle.net/KyleMuir/GeRVV/5/

于 2013-09-25T22:46:14.813 回答