代码之家  ›  专栏  ›  技术社区  ›  Veera

用于更改元素的类属性的Jquery事件

  •  1
  • Veera  · 技术社区  · 16 年前

    <div class="my-class"> . 因此,当一个新类被添加到这个div中时,(即。 <div class="my-class new-class">

    如何在JQuery中实现这一点?是否有属性更改事件?

    3 回复  |  直到 16 年前
        1
  •  2
  •   Igor Zinov'yev    16 年前

    $(document).ready(function(){
    
        // Cache the required element
        window.elementInQuestion = $('#element-that-changes-class');
    
        function checkClass(){
            if (window.elementInQuestion.hasClass('new-class')) {
                // Trigger a custom event
                $(document).trigger('elementHasClass');
    
                // If you need the check only until the first time the class appears
                // you can cancel further checks here
                clearInterval(window.classCheckerInterval);
            }
        }
    
        // Check if an element has class every once in a while
        window.classCheckerInterval = setInterval(checkClass, 100);
    
        $(document).bind('elementHasClass', function(){
            // Your handler here
        });
    });
    
        2
  •  0
  •   meder omuraliev    16 年前

    你不能把函数绑定到改变类的函数上吗?

        3
  •  0
  •   redsquare    16 年前

    没有跨浏览器节点更改事件。

    一种选择是使用 LiveQuery 插件。它将定期扫描dom以查找更改,如果遇到更改,它将引发一个事件。