Javascript 如何获取jQuery中文本节点内的锚文本
我正在做类似的事情来从子节点检索文本内容 一类的:Javascript 如何获取jQuery中文本节点内的锚文本,javascript,jquery,html,Javascript,Jquery,Html,我正在做类似的事情来从子节点检索文本内容 一类的: $(data).find(".mw-content-ltr").contents().each(function(_, row) { // ... } else if ( row.nodeType === 3 && $.trim(row.nodeValue).length ) { var textNodeValue = $.trim(row.nodeValue); console.lo
$(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.
当我需要这样的东西时:
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.
提前谢谢 试试这个:
$(“.mw content ltr”).text()
Fiddle将两者进行比较:
编辑:由于类中还有其他您不感兴趣的数据,您是否希望这项工作
$(".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);
}
});
jsiddle:试试这个:
$(“.mw content ltr”).text()
Fiddle将两者进行比较:
编辑:由于类中还有其他您不感兴趣的数据,您是否希望这项工作
$(".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);
}
});
JSFIDLE:文本节点中不能有
元素。谢谢你,这是非法的html,对吗@如果我对问题的理解是正确的,这是有效的HTML。Pointy的意思是,您不会看到
元素,因为您选择的是nodeType of 3(text)<代码>元素的节点类型为1。@布尔不,这不是非法的。文本节点仅为文本。当一个包含元素有文本和内联内容节点时,文本会被分解成单独的节点。文本节点内不能有
元素。谢谢,这是非法的html,对吗@如果我对问题的理解是正确的,这是有效的HTML。Pointy的意思是,您不会看到
元素,因为您选择的是nodeType of 3(text)<代码>元素的节点类型为1。@布尔不,这不是非法的。文本节点仅为文本。当一个包含元素有文本和内联内容节点时,文本被分解成单独的节点。。。谢谢Kyle,但我不能这样做,因为这会将所有文本内容带入类中,我需要在每个节点类型中进行自定义。您能提供更多上下文吗?很难知道什么是“数据”以及该类中还有什么。是的,该类中可能包含下一组元素:text、h2、p、h3等等。。。但是我需要根据每个节点类型做出不同的事情,因为我认为我不能只使用$(“.mw content ltr”).text()。这个问题与我之前问过的一个问题有关:@Boel我把它整理了一点,请看新的版本-它可能比switch语句更好:)嗯。。。谢谢Kyle,但我不能这样做,因为这会将所有文本内容带入类中,我需要在每个节点类型中进行自定义。您能提供更多上下文吗?很难知道什么是“数据”以及该类中还有什么。是的,该类中可能包含下一组元素:text、h2、p、h3等等。。。但是我需要根据每个节点类型做出不同的事情,因为我认为我不能只使用$(“.mw content ltr”).text()。这个问题与我之前问过的一个问题有关:@Boel我把它整理了一点,请看新的版本-它可能比switch语句更好:)