简体   繁体   English

使用jQuery从网页获取所有包含数字数据的元素?

[英]Getting all the elements containing numeric data from a webpage using jQuery?

I was wondering, is there any way that I can get all the elements ie div s span s etc containing the numeric data? 我想知道, 有什么方法可以获取所有包含数值数据的元素,即div s span s等吗? What I want to do is get all of them and then format them using a currency formatter jquery plugin. 我想要做的是将它们全部获取,然后使用货币格式化jquery插件对其进行格式化。 The code that I'm using now is: 我现在使用的代码是:

var options = {
    symbol: "",
    decimal: ".",
    thousand: ",",
    precision: 2,
    format: "%s%v"
};

$(".namount").each(function () {
    var formattedNum = accounting.formatMoney($(this).text(), options)
    $(this).html(formattedNum);
});

$(".totalsale").each(function(){
    var formattedNum = accounting.formatMoney($(this).text(), options)
    $(this).html(formattedNum);
});

$(".totalpurchase").each(function(){
    var formattedNum = accounting.formatMoney($(this).text(), options)
    $(this).html(formattedNum);
});    

There are 4 to 5 more similar calls to each loop for different elements. 对于不同的元素, each循环还有4至5个类似的调用。 The problem is, doing this won't be efficient. 问题是,这样做效率不高。 Is there any other way that I can achieve this. 还有其他方法可以实现这一目标。

Thanks 谢谢

Since you have a class selector this is one of the best ways. 由于您具有类选择器,因此这是最好的方法之一。 Only change I would do is to use a multiple selector and the use the .text() method to update the value in the element 我唯一要做的更改是使用多重选择器,并使用.text()方法更新元素中的值

$(".namount, .totalsale, .totalpurchase").text(function (idx, text) {
    return accounting.formatMoney(text, options)
});

Another possible change you can think of is to narrow down the search context using a parent container element 您可能想到的另一种可能的更改是使用父容器元素缩小搜索范围

You can do this by using class selector as suggested or you can achieve this using regular expression too. 您可以按照建议使用类选择器来完成此操作,也可以使用正则表达式来实现。

Refer this 推荐这个

jQuery selector regular expressions jQuery选择器正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM