Javascript 仅将类添加到HTML页面中的数字,链接除外
我在Joomla中尝试了这个脚本,为每个数字添加了一个类,这样我就可以用CSS格式化它们:Javascript 仅将类添加到HTML页面中的数字,链接除外,javascript,jquery,html,Javascript,Jquery,Html,我在Joomla中尝试了这个脚本,为每个数字添加了一个类,这样我就可以用CSS格式化它们: <script> (function($) { $(function() { $('h1,h2,h3,h4,h5,h6,p,strong,i,em,b,span,sup,sub').each(function() { var el = $(this), html = el.html(); html = html.r
<script>
(function($) {
$(function() {
$('h1,h2,h3,h4,h5,h6,p,strong,i,em,b,span,sup,sub').each(function() {
var el = $(this),
html = el.html();
html = html.replace(/(\d)/gi, "<span class='number'>$1</span>");
el.html(html);
});
});
})(jQuery);
</script>
在这种情况下,它会:
www.miosito.it/index.php&articleid=<span class="number">2</span>
www.miosito.it/index.php&articleid=2
因此,链接已断开。如何编辑脚本来修复它?一个更安全的策略是只在文本节点上运行正则表达式,而不是在原始html上运行正则表达式,正如一个更安全的策略是只在文本节点上运行正则表达式,而不是在原始html上运行正则表达式,如
www.miosito.it/index.php&articleid=<span class="number">2</span>