2

This is a continuation of 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>

How could one wrap all non-safe regions in an element with a disappear class (with jQuery).

Final Output

<h1>
  <span class="disappear">Let's say you had this </span>
  <span class="safe">text</span>
  <span class="disappear">.</span>
</h1>

That way, the parent node is still visible, but the non-safe regions disappear, leaving the safe.

I'm not sure how to do this, but surely it's possible.

4

4 に答える 4

6

テキスト ノードの anodeTypeは 3 です。ノードを反復しwrap()、テキスト ノードをラップするために使用します。

$someElement.contents().each(function() {
    if (this.nodeType == 3)
        $(this).wrap('<span class="disappear" />');
});

http://jsfiddle.net/SnjnJ/

于 2013-08-29T21:17:28.847 に答える
0

のすべての内容に一致し、選択からを削除するためにh1使用します。次に、それらを「消える」ように使用します。.not()safe.wrap()

http://api.jquery.com/not/

于 2013-08-29T21:15:56.647 に答える