开发者

jQuery Wrap innerHTML

开发者 https://www.devze.com 2023-02-18 13:37 出处:网络
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

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

精彩评论

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