开发者

How to surround this text that hasn't any HTML tags around it (with jQuery)?

<span id="subscription-toggle" class="list:subscription">

&nbsp;|&nbsp; <!-- I want to surround this with a div with a class -->

<span class="" id="subscribe-77" style="">
<a class="dim:subscription-toggle:subscribe-77:is-subscribed" href="http://localhost/taiwantalk2/topic/topic-with-two-tags-topic-with-two-tags-topic-with-two-tags/?action=bbp_subscribe&amp;topic_id=77&a开发者_如何学运维mp;amp;_wpnonce=25988e5dac">Subscribe</a>
</span>

</span>

So this should be the final result:

<span id="subscription-toggle" class="list:subscription">

<div class="hide-this">&nbsp;|&nbsp;</div>

<span class="" id="subscribe-77" style="">
<a class="dim:subscription-toggle:subscribe-77:is-subscribed" href="http://localhost/taiwantalk2/topic/topic-with-two-tags-topic-with-two-tags-topic-with-two-tags/?action=bbp_subscribe&amp;topic_id=77&amp;_wpnonce=25988e5dac">Subscribe</a>
</span>

</span>


If the text node you wish to wrap your div around is the only text node (i.e. it's the only text inside subscription-toggle that is not inside another element) then you can do this:

$("#subscription-toggle").contents().filter(function() {
    return this.nodeType == 3;
}).wrap("<div class='hide-this'></div>");

You can see an example here.

Alternatively, if there are multiple text nodes in the span, and you only want to wrap | characters, replace the body of the filter function with this line:

return $(this).text().indexOf("|") != -1;

You can see an example of that one working here.


I haven't tested but it should work:

var subsc = $('#subscription-toggle');
subsc.html(subsc.html().replace('/(&nbsp;\|&nbsp;)/g', '<div class="hide-this">$1</div>'));


You can achieve that with the wrap-function of jquery: http://api.jquery.com/wrap/

So you could search for the &nbsp;|&nbsp; with a :contains-Selector and than use the wrap-function.


how about:

$("#subscription-toggle").prepend(function(){ return $("").append(this.childNodes[0]); });

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜