为了账号安全,请及时绑定邮箱和手机立即绑定

当元素不再具有特定类时,我想调用一个函数

当元素不再具有特定类时,我想调用一个函数

一只斗牛犬 2021-06-08 14:27:00
我正在为一个只能使用 javascript 的网站制作插件。我想在 id = "gridview" 的 il 元素不再具有 "hidden" 类时执行代码。感谢您的帮助。我已经尝试过:document.getElementById("id="eval_grid_tab"").addEventListener("click", start);  这是 html<li id="eval_grid_tab"><a href="#gridview">Tabel</a></li><div id="gridview" class="rightsPanel smscTabsPanel hidden" style="height: 795px;">...</div>
查看完整描述

1 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

为此,您需要使用MutationObserver。它监视 DOM 的更改(例如删除/添加类)并触发回调函数供您以任何您认为合适的方式使用。


// Select the node that will be observed for mutations

var targetNode = document.getElementById('gridview');


// Options for the observer (which mutations to observe)

var config = { attributes: true };


// Callback function to execute when mutations are observed

var callback = function(mutationsList, observer) {

    for(var mutation of mutationsList) {

        if (mutation.type == 'attributes') {

            // Triggers when an attribute like 'class' is modified

            console.log('The ' + mutation.attributeName + ' attribute was modified.');

        }

    }

};


// Create an observer instance linked to the callback function

var observer = new MutationObserver(callback);


// Start observing the target node for configured mutations

observer.observe(targetNode, config);

稍后,您可以停止观察


observer.disconnect();


查看完整回答
反对 回复 2021-06-11
  • 1 回答
  • 0 关注
  • 113 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信