HTML DOM Text() nodes
HTML DOM Text() nodes
我需要获得元素的句柄,给定文本节点或Xpath的路径
我使用以下方法返回元素。result.singleNodeValue;
返回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]
这是我正在使用的html
<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>
似乎将文本节点拆分为3个部分<div class="fmtx">
的子节点
节点1
1843年至1848年间,狄更斯写了五部中篇小说或中长篇小说,并在圣诞节期间出版(
节点2
"圣诞颂歌"
节点3
《钟声》、《心上的蟋蟀》、《生命之战》和《闹鬼的人和鬼》™s Bargain)。故事不仅仅发生在圣诞节或新年™但包含了作者认为特别适合这个季节的主题。虽然圣诞节庆祝活动早于狄更斯,在他之前就有在圣诞节和年初讲鬼故事的传统,但狄更斯为这些庆祝活动和传统注入了新的、独特的活力,并将其发扬光大。他写了其他鬼故事,几乎都是恶搞或闹剧,但在他的"圣诞书"中,超自然元素赋予了他一种力量,让人物和读者从他们的社会厌世中觉醒。"
如何将这些文本节点组合为一个?
我尝试过
$('.fmtx').normalize();
,但没有成功,仍然显示了3个子节点。任何帮助都将不胜感激,谢谢!
更新
我添加了一个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
是的,这是显而易见的,因为在下面的代码中没有浏览器无法识别的测试
<div class="fmhT">
<b class="calibre3">INTRODUCTION</b>
</div>
仅此而已。
相关文章:
- 如何将输入(type=text)从html表单传递到javascript函数
- 如何使用nodes和.createElement(“b”)将文本加粗
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- 在文本区域中使用jQuery.text()保持换行符
- 用Javascript添加带有#text的tr元素
- 当用户按下回车键时,自动在text区域/text中插入消息
- 修正案'text'的元素
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- W3验证器->使用<脚本类型=“;text/javascript”>在html的正文中
- 我如何在INPUT TEXT中使用这个Javascript和jQuery
- <text区域>在我的html中包含event.keycode==13之后,wrap就不起作用了
- 从所有下拉菜单中选择val和text
- 节点,express应用程序返回text/javascript响应头(Content-Type),尽管设置了applic
- .val()返回未定义的.text返回随机代码
- JSTree's data.rslt.obj.text()返回一个文本数组,而不是所需节点的文本
- type=text/javascript和language=javascript之间的区别
- 如何使用tspan拆分Ext.draw.text中的长文本
- text()显示双文本
- getDocument by id/get input text from button dons'不起作用
- HTML DOM Text() nodes