开发者

jQuery Wrap innerHTML

i have html like this

<a href="#">like 开发者_运维技巧me on facebook</a>

and i want to wrap the text facebook so the result will be like this

<a href="#">like me on <span class="facebook-text">facebook</span></a>

how to do that in jQuery?


var html = $('a').html();
$('a').html(html.replace(/facebook/, '<span class="facebook-text">facebook</span>'));

and if you want to replace all occurrences of facebook within the string:

var html = $('a').html();
$('a').html(html.replace(/facebook/gi, '<span class="facebook-text">facebook</span>'));


Maybe you could use the $(...).contents() function to access the child nodes of your anchor tag.

http://api.jquery.com/contents/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜