获取NodeType的值并添加一些类

问题描述 投票:0回答:1

我的问题是,我有一个带整数的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的值。 “因为我需要将','的值更改为'逗号',才能将该类添加到我的div中。

有人可以帮助我哪种方法是最好的方法?

非常想,路易吉

javascript jquery html
1个回答
0
投票

最后在我的JS代码中添加一些IF以检测我的逗号,然后将“-逗号”类添加到:

$(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'));
});
© www.soinside.com 2019 - 2024. All rights reserved.