如何在span标记中包含元素的每个单词? [英] How to wrap each word of an element in a span tag?
本文介绍了如何在span标记中包含元素的每个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$("div.date")
.contents()
.filter(
function(){
return this.nodeType != 1;
})
.wrap("<span/>");
我是新手,并认为代码可以完成这个伎俩,但它包含了< span>
如下:
I am new and thought that code would have done the trick, but it wraps everything in the <span>
like so:
< div class ='date'>< ; span>
2011年12月22日< / span>< / div>
<div class='date'><span>
Dec 22, 2011</span></div>
它应该是这样的:
<div class='date'>
<span>Dec</span>
<span>22,</span>
<span>2011</span>
</div>
推荐答案
这会比这复杂一点。你将不得不找出所有单词并将它们重新附加到你的元素中,包裹在一个范围内。
It's gonna be a little more complicated than that. You're gonna have to find out all the words and re-append them to your element, wrapped in a span.
var words = $("p").text().split(" ");
$("p").empty();
$.each(words, function(i, v) {
$("p").append($("<span>").text(v));
});
这篇关于如何在span标记中包含元素的每个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文