js实现在当前class的父级的同级下添加容器元素

 

简述:<script>document.addEventListener("DOMContentLoaded", function() { // 每隔1秒执行一次 setInterval(function() { // 查找符合条件的 .npc-frame-price 元素 const priceElements = document.querySelectorAll(&#39;.npc-sidebar-container .npc-heading&#39;); priceElements.forEach(function(priceElement) { // 获取父级元素的父级容器 const parentElement = priceElement.parentElement; const siblingElement = parentElement.nextElementSibling; // 检查 .crown 是否已存在 if (!siblingElement || !siblingEle...

详情:

<script>

document.addEventListener("DOMContentLoaded", function() {

  // 每隔1秒执行一次

  setInterval(function() {

    // 查找符合条件的 .npc-frame-price 元素

    const priceElements = document.querySelectorAll('.npc-sidebar-container .npc-heading');


    priceElements.forEach(function(priceElement) {

      // 获取父级元素的父级容器

      const parentElement = priceElement.parentElement;

      const siblingElement = parentElement.nextElementSibling;


      // 检查 .crown 是否已存在

      if (!siblingElement || !siblingElement.classList.contains('crown')) {

        // 创建 .crown 元素

        const crownDiv = document.createElement('div');

        crownDiv.className = 'crown';


        // 创建 <span> 元素并添加图片

        const imgSpan = document.createElement('span');

        const img = document.createElement('img');

        img.src = 'https://img.icons8.com/?size=100&id=13728&format=png&color=000000';

        img.alt = '';

        imgSpan.appendChild(img);


        // 创建 <span> 元素并添加文本

        const textSpan = document.createElement('span');

        textSpan.textContent = 'Support Customization, Rapid Transportation, Three-year Warranty.';


        // 将 <span> 元素添加到 .crown 中

        crownDiv.appendChild(imgSpan);

        crownDiv.appendChild(textSpan);


        // 将 .crown 添加到父级的同级元素下

        parentElement.insertAdjacentElement('afterend', crownDiv);

      }

    });


  }, 1000); // 每秒检查一次

});


</script>