在给定的 DIV 中,我希望将 SPAN 应用于所有数字,并且仅应用于它们。有没有办法用 jQuery 选择数字?
jQuery 不提供本地文本选择器,但可以实现这种效果。我根据我的回答改编了这个上一个问题 https://stackoverflow.com/questions/3951755/transform-url-into-a-link-unless-there-already-was-a-link,将其设计为不会破坏 URL 中带有数字的任何链接(jsFiddle 演示 http://jsfiddle.net/EF4cR/2/):
function autospan() {
var exp = /-?[\d.]+/g;
$('*:not(script, style, textarea)').contents().each(function() {
if (this.nodeType == Node.TEXT_NODE) {
var textNode = $(this);
var span = $('<span/>').text(this.nodeValue);
span.html(span.html().replace(exp, '<span class="foo">$&</span>'));
textNode.replaceWith(span);
}
});
}
$(autospan);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)