我想用一些html标签替换段落中的每个单词,例如,考虑这个文本:
Lorem ipsum dolor sit amet,consectetuer adipiscing elit。
和用html标签替换单词后的结果,应该是:
<span class="word">Lorem</span> <span class="word">ipsum/span> <span class="word">dolor</span> <span class="word">sit</span> <span class="word">amet</span>, <span class="word">consectetuer</span> <span class="word">adipiscing</span> <span class="word">elit</span>.
需要一些指导如何实现这一目标。感谢。
答案 0 :(得分:1)
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit".split(" ").map(function (word) {
return '<span class="word">' +word +'</span>';
}).join("");
答案 1 :(得分:1)
var text = yourtext.split(' ');
for( var i = 0, len = text.length; i < len; i++ ) {
text[i] = '<span class="word">' + text[i] + '</span>';
}
});
在文本中,您将获得带有span标签的单词
详情请见:http://www.grasmash.com/article/jquery-wrap-each-word-element-unique-span-tag#sthash.y0DkyzDW.dpuf
答案 2 :(得分:1)
你这样做:
<script>
var x="Lorem ipsum dolor sit amet, consectetuer adipiscing elit".split(" ").map(function (word) {
return '<span class="word">' +word +'</span>';
}).join(" ");
document.getElementById('htmlForm').innerHTML=x;
</script>
<p id="htmlForm"></p>