Visionary Awkward Logo

勝手にclass制御の動的なやつに追従してなんかやりたい時

Posted on

勝手にclass制御の動的なやつに追従してなんかやりたい時に暴挙

<ul id="ul">
    <li>
        <p>testtest</p>
    </li>
</ul>
<script>
window.onload = function(){
    fetchDom();
}
function fetchDom() {
    let parent = document.getElementById("ul"); //ul親
    let children = parent.getElementsByTagName("li"); //li子
    children = Array.from(children); //HTMLCollection→forEach
    const mo = new MutationObserver(records => {
        children.forEach(function (elm) {
            if (elm.classList.contains("target")) {
                let word = elm.getElementsByTagName("p")[0];
                word.innerText = "test";
            }
        });
    });
    mo.observe(parent, {
        attributes: true, //属性監視
        subtree: true //子孫監視
    });
}
</script>
最近の投稿