我想在元素不再具有特定类(没有jquery)时调用函数

时间:2019-05-26 00:27:12

标签: javascript class hidden

我正在为只能使用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 个答案:

答案 0 :(得分:0)

为此,您需要使用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();