如何使用jQuery将HTML环绕在文本节点周围? [英] How to wrap HTML around a text node with jQuery?
本文介绍了如何使用jQuery将HTML环绕在文本节点周围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<div class="pagination">
<a href="#">1</a>
<a href="#">2</a>
3
<a href="#">4</a>
</div>
我需要使用jQuery将HTML元素包装在数字"3"周围.我无法修改HTML,因为它是动态生成的.
I need to use jQuery to wrap an HTML element around the number "3". I can't modify the HTML because it is dynamically generated.
推荐答案
这会将您的所有数字括起来,因此结构为:
this will wrap all your number with a span, so the structure will be :
<a><span>1</span></a>
并且所选的一个唯一跨度不带:
and the selected one only span without a :
<span>2</span>
var html = $('.pagination').html();
html = html.replace(/[0-9]/g,"<span>$&</span>");
$('.pagination').html(html);
因此您可以轻松地使用:
so you can easily style it with :
.pagination a span {normal style}
.pagination span{selected style}
这篇关于如何使用jQuery将HTML环绕在文本节点周围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文