简体   繁体   English

将javascript函数应用于html元素

[英]Apply javascript function to an html element

Sorry for the noob question but I am trying to apply this javascript function to data in an html table. 很抱歉noob问题,但我正在尝试将此javascript函数应用于html表中的数据。 The function comes from this Stack Overflow post . 该函数来自此Stack Overflow帖子

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

I wish to apply this function to something like this: 我希望将此函数应用于以下内容:

<table>
    <tr>
        <td>5000</td>
        <td>5600</td>
    </tr>
</table>

with the hopes of returning 5,000 next to 5,600. 希望在5,600点附近回归5,000。 I can't figure out this simple thing. 我无法弄清楚这个简单的事情。 I know I need to use an id or class on one of the tags and that this needs to match something in the function so I tried 我知道我需要在其中一个标签上使用id或类,并且这需要匹配函数中的某些内容,所以我尝试了

<table>
    <tr class="numberWithCommas">
        <td>5000</td>
        <td>5600</td>
    </tr>
</table>

to no avail. 无济于事。 I'm sure the function works given the amount of upvotes. 我确定这个功能在给定量的情况下有效。

Can a kind soul please walk me through the steps to make this work? 善良的灵魂能指导我完成这项工作的步骤吗? I've googled all over for a basic tutorial and haven't found anything. 我已经谷歌搜索了一个基本的教程,但没有找到任何东西。

You first need to get all the td s, call the function on the content and insert the result back into the cell. 首先需要获取所有td ,在内容上调用函数并将结果插回到单元格中。

HTML HTML

<table>
    <tr class="numberWithCommas">
        <td>5000</td>
        <td>5600</td>
    </tr>
</table>

Javascript 使用Javascript

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

var elements = document.querySelectorAll('.numberWithCommas td'),
    i;

for (i in elements) {
    if (elements[i].innerHTML !== undefined) {
        elements[i].innerHTML = numberWithCommas(elements[i].innerHTML);
    }
}

Demo 演示

To explain all this code: 要解释所有这些代码:

This is your function: 这是你的功能:

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

This allows us to get all the td within all tr s with numberWithCommas as a class and sets them all as an array in the javascript variable elements : 这允许我们使用numberWithCommas作为类获取所有tr的所有td ,并将它们全部设置为javascript变量elements的数组:

var elements = document.querySelectorAll('.numberWithCommas td'),
    i;

We then loop through the array accessing one td at a time: 然后我们循环访问一次访问一个td的数组:

for (i in elements) {
    ...
}

Inside the for loop we check that the value of the cell is not empty: 在for循环中,我们检查单元格的值是否为空:

if (elements[i].innerHTML !== undefined) {
    ...
}

Inside the if statement we call the function numberWithCommas() and pass it the value of elements[i].innerHTML . if语句中,我们调用函数numberWithCommas()并将elements[i].innerHTML的值传递给它。 We then get back the resulting string and apply it to elements[i].innerHTML : 然后我们返回结果字符串并将其应用于elements[i].innerHTML

elements[i].innerHTML = numberWithCommas(elements[i].innerHTML);

You need to iterate over each td and apply this function.. 你需要迭代each td并应用这个函数..

var tr = document.getElementsByClassName('numberWithCommas');

for (var i = 0, len = tr.length; i < len; i++) {
    for(var j=0,len1 = tr[i].children.length;j<len1; j++){
        var td = tr[i].children[j];
        td.innerHTML= numberWithCommas(td.innerHTML)
    }
}​

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};

Check Fiddle 检查小提琴

Using jQuery 使用jQuery

$('table tr.numberWithCommas').each(function(){
    var $tr = $(this);
    $tr.find('td').each(function(){
        var $td = $(this);
        $td.html(function(){
            return  numberWithCommas(this.innerHTML);
        });
    });        
});

jQuery Fiddle jQuery小提琴

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

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