用jQuery选择数字 [英] Selecting numbers with jQuery
本文介绍了用jQuery选择数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在给定的DIV中,我希望将SPAN应用于所有数字,并且仅应用于它们.有没有一种方法可以使用jQuery选择数字?
In a given DIV I wish to apply a SPAN to all numbers and only them. Is there a way to select numbers with jQuery ?
推荐答案
jQuery不提供文本的本机选择器,但可以实现这种效果.我将此答案改编为上一个问题,将其设计为不破坏URL中带有数字的任何链接( jsFiddle演示) :
jQuery doesn't provide native selectors for text, but it is possible to achieve that effect. I adapted this from my answer to a previous question, designing it to not mangle any links with numbers in the URL (jsFiddle demo):
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);
这篇关于用jQuery选择数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文