jQuery:找到一个字符串并将其包装在< a>标签? [英] jquery: find a string and wrap it in an <a> tag?
本文介绍了jQuery:找到一个字符串并将其包装在< a>标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个:
<div class="" id="fancy_hover">
<a href="home.html" id="start_writer"></a>
<div>
<p class="hidden" style="display: block;">click here to see this event with all the bells and whistles</p>
</div>
</div>
我需要做一个.find()
的等效项,但是对于字符串"click here"
而不是节点,我需要将其包装在<a>
标记中.
I need to do an equivalent of .find()
but for the string "click here"
instead of a node, i then need to wrap it in an <a>
tag.
jquery的最佳方法是什么?
Whats the best approach with jquery?
推荐答案
使用:contains
过滤器选择器:
$('p.hidden:contains("click here")')
要将其包装在链接中:
$('p.hidden:contains("click here")').html()
.replace('click here', '<a href="url">click here</a>');
要将整个文本包装在链接中:
$('p.hidden:contains("click here")')
.html('<a href="url">' + $('p.hidden:contains("click here")').text() + '<a>');
更多信息:
这篇关于jQuery:找到一个字符串并将其包装在< a>标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文