第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

實(shí)時(shí)檢測(cè) DOM 自定義數(shù)據(jù)屬性值的變化,并在發(fā)生變化時(shí)使用 js 運(yùn)行函數(shù)

實(shí)時(shí)檢測(cè) DOM 自定義數(shù)據(jù)屬性值的變化,并在發(fā)生變化時(shí)使用 js 運(yùn)行函數(shù)

縹緲止盈 2022-01-13 15:39:05
每次 DOM 元素的自定義數(shù)據(jù)屬性的值發(fā)生變化時(shí),我都會(huì)嘗試運(yùn)行一個(gè)簡(jiǎn)單的函數(shù)。下面是一個(gè)例子<div id="myDiv" data-type="type-1">    <!-- Some Content --></div>在上面的 HTML 代碼中,我有div一個(gè)自定義數(shù)據(jù)屬性,data-type其值是我使用 javascript 更改的。當(dāng)屬性的值根據(jù)它所持有的值而改變時(shí),我想啟動(dòng)另一個(gè)函數(shù)。例如使用 if 語(yǔ)句(不起作用!??)var myDiv = document.getElementById("myDiv");var myDivAttr = myDiv.getAttribute("data-type");if(myDivAttr == "type-1"){   typeOneFunction();}else if(myDivAttr == "type-2"){   typeTwoFunction();}// and so on...我希望我的問(wèn)題足夠清楚????
查看完整描述

1 回答

?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

您可以使用Mutation Observers實(shí)現(xiàn)此目的


// Select the node that will be observed for mutations

const targetNode = document.getElementById('myDiv');


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

const config = { attributes: true };


// Callback function to execute when mutations are observed

const callback = function(mutationsList, observer) {

    // Use traditional 'for loops' for IE 11

    for(let mutation of mutationsList) {

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

            if(myDivAttr == "type-1"){

              typeOneFunction();

             }

             else if(myDivAttr == "type-2"){

               typeTwoFunction();

             }

        }

    }

};


// Create an observer instance linked to the callback function

const observer = new MutationObserver(callback);


// Start observing the target node for configured mutations

observer.observe(targetNode, config);


// Later, you can stop observing

observer.disconnect();

https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver


我沒(méi)有測(cè)試該代碼*


查看完整回答
反對(duì) 回復(fù) 2022-01-13
  • 1 回答
  • 0 關(guān)注
  • 231 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)