从两个元素之间获取XML中的html和文本内容

时间:2013-08-01 13:46:19

标签: javascript

我已经阅读了所有以前的条目,以获取两个元素之间的html /文本节点内容,但它不适用于我。

我尝试过jQuery和纯javascript的混合,但一无所获。

我正在尝试使用xml代码。我无法控制xml - 它直接来自世界英语圣经XML。

var xml='<p><v id="1" />In the beginning, God<f caller="+">The Hebrew word rendered “God” is “אֱלֹהִ֑ים” (Elohim).</f> created the heavens and the earth. <ve />'
+'<v id="2" />The earth was formless and empty. Darkness was on the surface of the deep and God’s Spirit was hovering over the surface of the waters. <ve /></p>'
+'<p><v id="3" />God said, “Let there be light,” and there was light. <ve />'
+'<v id="4" />God saw the light, and saw that it was good. God divided the light from the darkness. <ve />'
+'<v id="5" />God called the light “day”, and the darkness he called “night”. There was evening and there was morning, the first day. <ve /></p>';

我需要做的是获取<v>元素和<ve>元素之间的所有html(文本和html)并将其包装在<span>中以便我可以使用css关于个别经文。

//original
<v id="1" />In the beginning, God<f caller="+">The Hebrew word rendered “God” is “אֱלֹהִ֑ים” (Elohim).</f> created the heavens and the earth. <ve />
//becomes
<span class=v id=GEN0101>In the beginning, God<f caller="+">The Hebrew word rendered “God” is “אֱלֹהִ֑ים” (Elohim).</f> created the heavens and the earth. </span>

我使用nextUntil()尝试了jQuery,但它只修改了<f>元素。

var $x=$(xml);
$x.find("v").nextUntil("ve").css('background','red');
$("#content").append($x);

这是我的小提琴:JSFiddle work

我尝试过纯粹的javascript,而且我也无法使用它。

var v = xml.getElementsByTagName("ve")[0].previousSibling.textContent;
$("#content").append('<div>'+v+'</div>');

谢谢

1 个答案:

答案 0 :(得分:0)

我能想到的最好的就是使用jQuery的contents()函数。看看这里的小提琴:http://jsfiddle.net/DKXMx/