Добавление цифры к заголовку Н2 на странице через JS na elementor

cjplfybt

document.addEventListener("DOMContentLoaded", function () {
  if (document.body.classList.contains("single-post")) {
    let sectionCounter = 0;
    let subsectionCounter = 0;
    
    const container = document.querySelector("#page.hfeed.site");
    
    if (!container) {
      return; // Если контейнер не найден, прекращаем выполнение
    }

    function processHeadings(element) {
      const children = Array.from(element.childNodes);
      children.forEach((child) => {
        if (child.nodeType === Node.ELEMENT_NODE) {
          if (child.tagName === "H2") {
            sectionCounter++;
            subsectionCounter = 0; // Сбрасываем счётчик для H3
          } else if (child.tagName === "H3") {
            subsectionCounter++;
            const counterText = `${subsectionCounter}`;
            child.style.setProperty("--counter", `"${counterText}"`);
          }
          processHeadings(child);
        }
      });
    }

    processHeadings(container);
  }
});

 

 

cjdhfydf

 

Теги:
Содержимое
Вам может быть интересно

Скрол к блоку на странице (без перекрытия)

Маска на поле телефон

Форма Elementor

Let's work together!