简体   繁体   English

如何引用div标签的Javascript函数来添加逗号?

[英]How do I reference Javascript function for div tag to add commas?

I am a Javascript newbie ... and I've heard of jQuery, but don't have a clue how to use it, so please bear with my complete lack of knowledge here ... :) 我是一个Javascript新手...我听说过jQuery,但是不知道如何使用它,所以请在这里承担我完全缺乏的知识...... :)

I am dynamically importing information from an Excel database onto a website. 我正在将信息从Excel数据库动态导入到网站上。 It works great, except that none of the numbers have commas - it's a formatting issue that I can't change at the source, so I need to change it in the website code. 它工作得很好,除了没有数字有逗号 - 这是一个格式化问题,我无法在源头更改,所以我需要在网站代码中更改它。

The relevant HTML code looks like this: 相关的HTML代码如下所示:

<tr><td class="num">191025</td><td>Stuff</td><td class="num">$60</td></tr>
<tr><td class="num">184160</td><td>Other Stuff</td><td class="num">$15</td></tr>

I want to use Javascript (or jQuery - whatever works) to add commas to all existing "num" classes (so 191025 would appear as 191,025). 我想使用Javascript(或jQuery - 无论什么工作)将逗号添加到所有现有的“num”类(因此191025将显示为191,025)。

I have been poking around this site and others and found this site: http://www.mredkj.com/javascript/nfbasic.html which has this public domain code: 我一直在寻找这个网站和其他人,发现这个网站: http//www.mredkj.com/javascript/nfbasic.html有这个公共域代码:

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

Which seems great, and I added it to my existing external .js page that I reference within my html page. 这看起来很棒,我把它添加到我在html页面中引用的现有外部.js页面。

However, the issue is now how do I use the code? 但是,现在的问题是如何使用代码? Looking at the examples on that page, it looks like I would have to do something like this: 查看该页面上的示例,看起来我必须做这样的事情:

<tr><td class="num">addCommas(191025)</td><td>Stuff</td><td class="num">$60</td></tr>

And that's just not realistic - there are hundreds of these numbers, and I just can't update each and every one of them individually. 这是不现实的 - 有数百个这样的数字,我不能单独更新它们中的每一个。

I am certain that there's a way that Javascript (or jQuery?) can automatically apply coding to the existing "num" classes to insert commas ... but I have no idea how to go about it. 我确信Javascript(或jQuery?)有一种方法可以自动将编码应用到现有的“num”类来插入逗号......但我不知道如何去做。 So, all of this bring me to my question: How do I efficiently apply this (or other - better?) code to the div class so that commas are added? 所以,所有这些都让我想到了一个问题: 如何有效地将这个(或其他 - 更好?)代码应用于div类,以便添加逗号?

I greatly appreciate any help ... but please keep in mind my newbie-ness, and let me know what needs to go in the external .js sheet & what goes on the .html page. 非常感谢任何帮助...但请记住我的新手,并让我知道外部.js表格需要进行什么以及.html页面上的内容。

Thank you!! 谢谢!!

jQuery solution: jQuery解决方案:

$('.num').each(function () {
    $(this).text(addCommas($(this).text()));
});​​​​​​​​​​

javascript solution (won't work in IE as it does not support getElementsByClassName ) JavaScript解决方案(在IE中不起作用,因为它不支持getElementsByClassName

var elements = document.getElementsByClassName('num');
var textPropertyToUse; // cross-browser support

for (var i = 0; i < elements.length; i++) {
    textPropertyToUse = 'textContent' in elements[i] ? 
        'textContent' : 
        'innerText';

    elements[i][textPropertyToUse] = addCommas(elements[i][textPropertyToUse]);
}

Working example: http://jsfiddle.net/XpbJV/ 工作示例: http//jsfiddle.net/XpbJV/

Execute this code on page load: 在页面加载时执行此代码:

$(".num").each(function() {
   var self = $(this), text = self.text();
   self.text(addCommas(text));
});

A very simple jQuery solution would be... 一个非常简单的jQuery解决方案将是......

$(".num").text(addCommas);

...as long as you change your addCommas function signature to this... ...只要你将addCommas函数签名改为此...

function addCommas(i, nStr)

This will work because your addCommas would take the current text content as the second argument, and return the updated text content. 这将起作用,因为您的addCommas将当前文本内容作为第二个参数,并返回更新的文本内容。 That's how most of jQuery's iterator versions of their methods work. 这就是jQuery的大多数迭代器版本的方法都有效的方法。

DEMO: http://jsfiddle.net/gprf6/ 演示: http //jsfiddle.net/gprf6/

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

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