亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當元素不再具有特定類時,我想調用一個函數

當元素不再具有特定類時,我想調用一個函數

一只斗牛犬 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 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號