HTML DOM Text() 节点

3

我需要根据文本节点或XPath路径来掌握元素。以下是返回该元素的方法。

result.singleNodeValue;如果存在多个childNodes,则返回null,我能否将它们合并为一个?

function xpathElement(expr) {
    var resolver = function (prefix) {
        if ("xhtml" == prefix) {
            return "http://www.w3.org/1999/xhtml";
        }
    }

    var result = document.evaluate(expr, document, resolver, 9, null)

    console.log("function xpathElement: possible multiple nodes:");
    console.log(result);
    result = result.singleNodeValue;
    console.log("function xpathElement: singleNodeValue:");
    console.log(result);


    return result;
}



工作原理:
当我传入以下内容时,它能够很好地返回文本节点

/xhtml:html[0001]/xhtml:body[0001]/xhtml:div[0002]/xhtml:div[0001]/text()[0001]



无法正常工作:
当我传入以下内容时,返回 null

/xhtml:html[0001]/xhtml:body[0001]/xhtml:div[0002]/text()[0003]



Here is the html I am using

<p class="calibre1">
  <a id="itr"></a>
</p>
<div class="fmhT">
  <b class="calibre3">INTRODUCTION</b>
</div>
<div class="fmtx">
  Between 1843 and 1848, Dickens wrote five novellas or long short stories that he
 published at Christmastime (<i class="calibre6">A Christmas Carol</i>, The Chimes, The
 Cricket on the Hearth, The Battle of Life, and The Haunted Man and the Ghost’s Bargain).
 The stories are not merely set at Christmas or the New Year’s holiday but contain themes
 the author felt were particularly appropriate to the season. While Christmas celebrations
 predate Dickens and there existed before him a tradition of telling ghost-tales at
 Christmas and the turn of the year, Dickens breathed a new and unique vigor into these
 celebrations and traditions that carry forward to this day. He wrote other ghost stories,
 almost all of which are spoofs or farces, but in his “Christmas books” allowed 
supernatural elements a power to awaken characters and readers from their social 
misanthropy.

</div>

看起来这个元素<i class="calibre6">A Christmas Carol</i>将文本节点分成了三部分。

<div class="fmtx">的子节点

节点1
"在1843年至1848年间,狄更斯写了五篇中短篇小说,他在圣诞节期间出版了这些小说(“

节点2
“A Christmas Carol”

节点3
", The Chimes, The Cricket on the Hearth, The Battle of Life, and The Haunted Man and the Ghost’s Bargain)。这些故事不仅仅是在圣诞或新年假期设定,而且包含了作者认为特别适合这个季节的主题。虽然圣诞庆祝活动早于狄更斯,并且在他之前存在一种在圣诞和年末讲鬼故事的传统,但狄更斯为这些庆祝活动和传统注入了新的和独特的活力,这些传统一直延续到今天。他写了其他的鬼故事,几乎都是恶搞或闹剧,但在他的“圣诞书”中,超自然元素被赋予了唤醒人物和读者摆脱社交厌恶的力量。"


如何将这些文本节点合并为一个?

我尝试过$('.fmtx').normalize();,但没有成功,仍然显示3个childNodes。

非常感谢任何帮助!

更新

我添加了一个jsfiddle,也许这会更有意义,http://jsfiddle.net/95Bc7/

请关注底部的控制台日志,以获取+++++++++++++++++ ancestor xpath element: null的信息。

1) 在斜体文本之前进行选择。

+++++++++++++++++ ancestor xpath element: [object Text]



2) 在斜体文字上进行选择

+++++++++++++++++ ancestor xpath element: [object Text]



3) 在斜体文本之后进行选择

+++++++++++++++++ ancestor xpath element: null

1
这是复制粘贴的问题吗?你的“工作”和“不工作”的例子完全一样。 - Kevin B
不确定你是否正确理解了,我需要返回DOM文本节点元素。那个<i></i>将节点分成3部分,因此结果singleNodeValue返回null。我能将这些分割的文本节点合并成一个吗,以便result.singleNodeValue返回该元素? - Francois
让我感到困惑的是,文本节点不能包含<i>节点,而您似乎希望将其作为文本节点的一部分。 - Kevin B
jQuery没有.normalize()方法。那是一个本地方法,但只能用于连接相邻的文本节点,而不是元素。 - Blue Skies
你想要一个节点而不是文本,有什么原因吗? - Blue Skies
显示剩余9条评论
1个回答

0

是的,这很明显,因为在下面的代码中没有任何测试无法被浏览器识别

<div class="fmhT">
  <b class="calibre3">INTRODUCTION</b>
</div>

就这些。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接