这是 What's a good way to show parts of an element but hide the rest? 的延续
<h1>
Let's say you had this <span class="safe">text</span>.
</h1>
如何使用 disappear
类(使用 jQuery)将所有非安全
区域包装在一个元素中。
最终输出
<h1>
<span class="disappear">Let's say you had this </span>
<span class="safe">text</span>
<span class="disappear">.</span>
</h1>
这样,父节点
仍然可见,但非安全
区域消失,留下安全
。
我不知道如何做到这一点,但肯定是可能的。
最佳答案
文本节点的 nodeType
为 3。迭代节点并使用 wrap()
来包装文本节点。:
$someElement.contents().each(function() {
if (this.nodeType == 3)
$(this).wrap('<span class="disappear" />');
});
关于javascript - 如何将跨度添加到不受限制的节点的所有区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18521059/