类更改的事件触发器 [英] Event trigger on a class change

查看:176
本文介绍了类更改的事件触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当包含触发器类的div标签发生更改时,我希望我的活动触发

I'd like my event to be triggered when a div tag containing a trigger class is changed.

我不知道如何让它听取课程的'添加事件。

I have no idea how to make it listen to the class' adding event.



<div id="test">test</div>

<script type="text/javascript">
    document.getElementById.setAttribute("class", "trigger");

    function workOnClassAdd() {
       alert("I'm triggered");
    }
</script>


推荐答案

那么突变事件,但它们已被弃用,未来会有Mutation Observers,但很长一段时间内它们都不会得到完全支持。那么你能做些什么呢?

Well there were mutation events, but they were deprecated and the future there will be Mutation Observers, but they will not be fully supported for a long time. So what can you do in the mean time?

你可以用一个计时器来检查元素。

You can use a timer to check the element.

function addClassNameListener(elemId, callback) {
    var elem = document.getElementById(elemId);
    var lastClassName = elem.className;
    window.setInterval( function() {   
       var className = elem.className;
        if (className !== lastClassName) {
            callback();   
            lastClassName = className;
        }
    },10);
}

运行示例: jsFiddle

这篇关于类更改的事件触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆