当价格内容改变JQuery DOMCharacterDataModified时,将类添加到元素

时间:2014-02-18 02:20:59

标签: jquery

我想在$ value更改时向价格范围添加一个类。添加产品时为客户提供更好的视觉线索。

尝试:

$(document).ready(function() {
    $('.cart-price').bind('DOMCharacterDataModified', function() {
        $('.cart-price').addClass('change');

    var delay = setTimeout(function(){
        $('.cart-price').removeClass('change');
    }, 1100)
    });
});

CSS:

.cart-price.change
{
    color:#F00;
}

1 个答案:

答案 0 :(得分:2)

最好使用mutation observers因为mutation events已被弃用。但要注意browser compatibility - IE< 11不支持

$(document).ready(function () {
    var $cartprice = $('.cart-price');

    if ('MutationObserver' in window) {
        var observer = new MutationObserver(highlight);
        observer.observe($cartprice[0], {
            childList: true
        });
    } else {
        //for browsers which does not support MutationObserver
        $cartprice.on('DOMSubtreeModified', highlight)
    }

    function highlight() {
        $cartprice.addClass('change');

        var delay = setTimeout(function () {
            $cartprice.removeClass('change');
        }, 1100)
    }
});

演示:Fiddle