admin管理员组文章数量:1310160
I have some non-static content on my webpage and I need all unwrapped plain text to be wrapped inside an anchor element with a class, however, I need to maintain the placement of that text.
I've search around on this site and found questions like these which are asking the same question except the text is always in the beginning or the end and the answers always prepend/append the content back into the <div>
.
To make the question even more plicated, there are scenarios where the content will be only unwrapped plain text and I'll need to wrap that as well.
My HTML:
<div>
<a>1</a>
<a>2</a>
3
<a>4</a>
</div>
Sometimes:
<div>
1
</div>
I've tried all the answers on this page but they all reorder the text.
Any ideas?
I have some non-static content on my webpage and I need all unwrapped plain text to be wrapped inside an anchor element with a class, however, I need to maintain the placement of that text.
I've search around on this site and found questions like these which are asking the same question except the text is always in the beginning or the end and the answers always prepend/append the content back into the <div>
.
To make the question even more plicated, there are scenarios where the content will be only unwrapped plain text and I'll need to wrap that as well.
My HTML:
<div>
<a>1</a>
<a>2</a>
3
<a>4</a>
</div>
Sometimes:
<div>
1
</div>
I've tried all the answers on this page but they all reorder the text.
Any ideas?
Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Nov 25, 2013 at 1:11 henryaaronhenryaaron 6,20221 gold badges63 silver badges81 bronze badges1 Answer
Reset to default 14Iterate over all text nodes of the element:
$('div').contents().filter(function() {
return this.nodeType === 3;
}).wrap('<a class="awesomeClass"></a>');
DEMO
.contents()
retrieves all child nodes of the element, not only element nodes.
The .filter
callback discards all nodes that are not text nodes. It works as follows:
Each DOM node has the property nodeType
which specifies its type (what surprise). This value is a constant. Element nodes have the value 1
and text nodes have the value 3
. .filter
will remove all elements from the set for which the callback returns false
.
Then each of the text nodes is wrapped in a new element.
I'm having a whitespace problem.
If your HTML looks like
<div>
1
</div>
then the element has one child node, a text node. The value of the text node does not only consist of the visible character(s), but also of all the whitespace characters, e.g. the line break following the opening tag. Here they are made visible (⋅
is a space, ¬
is a line break):
<div>¬
⋅⋅1¬
</div>
The value of the text node is therefore ¬⋅⋅1¬
.
A way to solve this would be to trim the node values, removing trailing and preceding whitespace character:
$('div').contents().filter(function() {
return this.nodeType === 3;
}).each(function() {
this.nodeValue = $.trim(this.nodeValue);
}).wrap('<a class="awesomeClass"></a>');
DEMO
本文标签: jqueryJavaScript wrapping unwrapped plain textStack Overflow
版权声明:本文标题:jquery - JavaScript wrapping unwrapped plain text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741838744a2400371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论