获取NodeType的值并添加一些类

时间:2015-01-28 08:07:00

标签: javascript jquery html

我的问题是我有一个带有任何整数的div,我想用每个数字替换它。

在这里,你是我的jsfiddle:http://jsfiddle.net/christianMM/qns7ohx2/

我的HTML代码是:

<div class="precios price2">99,99</div>

我的JS代码是:

$(function () { 
    function wrapCharacters() {
        $('.precios').contents().each(function () {
            if (this.nodeType === 1) {
                wrapCharacters(this);
            } else if (this.nodeType === 3) {
                $(this).replaceWith($.map(this.nodeValue.split(''), function (c) {
                    return '<span class="big p' + c + '">' + c + '</span>';
                }).join(''));
            }
        });
    }
    wrapCharacters($('.precios'));
});

多数人现在正在工作,但我无法获得 c.NodeValue 的价值。 &#39;因为我需要更改&#39;,&#39;的值到“逗号”,将此课程添加到我的div。

有人可以帮助我哪一个是获得它的最好方法?

很多, Luiggi

1 个答案:

答案 0 :(得分:0)

最后在我的JS代码中添加一些IF以检测我的逗号,并添加&#34; -comma&#34;上课:

        $(document).ready(function(){

        function wrapCharacters() {
            $('.precios').contents().each(function() {
                if(this.nodeType === 1) {
                    wrapCharacters(this);
                }
                else if(this.nodeType === 3) {
                    $(this).replaceWith($.map(this.nodeValue.split(''), function(c) {

                        if (c == ',') {
                            c='-comma';
                        return '<span class="big p' + c +'">' + c + '</span>';
                        }else{
                        return '<span class="big p' + c +'">' + c + '</span>';

                        }
                    }).join(''));
                }
            });
        }    


        wrapCharacters($('.precios'));

});