使用 forEach 函数添加多个工具提示不起作用

所以我知道一个解决这个问题的方法,但是为了了解更多关于 JavaScript 功能的信息,我会在这里展示它,所以也许有人可以向我解释为什么这不起作用。


 <li>

        <a href="#" class="kooltip" data-message="Početna">

          <i class="fas fa-home nav__link" ></i>

        </a>

      </li>


      <li>

        <a href="#" class="kooltip" data-message="O nama" >

          <i class="fas fa-address-card nav__link" ></i>

        </a>

      </li>

      <li>

        <a href="#" class="kooltip" data-message="Usluge">

          <i class="fas fa-notes-medical nav__link" ></i>

        </a>

      </li>

      <li>

        <a href="#" class="kooltip" data-message="Kontakt" >

          <i class="fas fa-mail-bulk nav__link" ></i>

        </a>

      </li>






class Tooltip {

    constructor(element){

        this.element = element

        this.message= element.getAttribute("data-message")

    }

    init() {

        const tip = document.createElement("div")

        tip.classList.add("tip")

        tip.textContent = this.message

        this.element.appendChild(tip)


        this.element.addEventListener("mouseenter", () => {

            tip.classList.add("active")

        })

        this.element.addEventListener("mouseleave", () => {

            tip.classList.remove("active")

        })


    }

}



const addTooltips = () => {

    const kooltip = document.querySelectorAll(".kooltip")

    kooltip.forEach(tip => {

       new Tooltip(tip)  

        tip.init()

    }) 

}


addTooltips()

我想为 html 中的每个 a 标签添加工具提示,但由于某种原因,这不起作用,我没有错误消息来获取有关原因的信息。


德玛西亚99
浏览 274回答 1
1回答

慕勒3428872

您的问题在 内addTooltips,这是一个改进版本:const addTooltips = () => {&nbsp; const kooltips = document.querySelectorAll('a.kooltip')&nbsp; kooltips.forEach(kooltip => {&nbsp; &nbsp; const tip = new Tooltip(kooltip)&nbsp; &nbsp; tip.init()&nbsp; })}addTooltips()这是一个工作示例:class Tooltip {&nbsp; constructor(element){&nbsp; &nbsp; this.element = element&nbsp; &nbsp; this.message= element.getAttribute("data-message")&nbsp; }&nbsp;&nbsp;&nbsp; init() {&nbsp; &nbsp; const tip = document.createElement("div")&nbsp; &nbsp; tip.classList.add("tip")&nbsp; &nbsp; tip.textContent = this.message&nbsp; &nbsp; this.element.appendChild(tip)&nbsp; &nbsp; this.element.addEventListener("mouseenter", () => {&nbsp; &nbsp; &nbsp; &nbsp; tip.classList.add("active")&nbsp; &nbsp; })&nbsp; &nbsp; this.element.addEventListener("mouseleave", () => {&nbsp; &nbsp; &nbsp; &nbsp; tip.classList.remove("active")&nbsp; &nbsp; })&nbsp; }}const addTooltips = () => {&nbsp; const kooltips = document.querySelectorAll('a.kooltip')&nbsp;&nbsp;&nbsp; kooltips.forEach(kt => {&nbsp; &nbsp; const tip = new Tooltip(kt)&nbsp; &nbsp; tip.init()&nbsp; })}addTooltips()<!DOCTYPE html><html><head>&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width">&nbsp; <title>JS Bin</title></head><body>&nbsp; <li>&nbsp; &nbsp; <a href="#" class="kooltip" data-message="Početna">&nbsp; &nbsp; &nbsp; <i class="fas fa-home nav__link" ></i>&nbsp; &nbsp; </a>&nbsp; </li>&nbsp; <li>&nbsp; &nbsp; <a href="#" class="kooltip" data-message="O nama" >&nbsp; &nbsp; &nbsp; <i class="fas fa-address-card nav__link" ></i>&nbsp; &nbsp; </a>&nbsp; </li>&nbsp; <li>&nbsp; &nbsp; <a href="#" class="kooltip" data-message="Usluge">&nbsp; &nbsp; &nbsp; <i class="fas fa-notes-medical nav__link" ></i>&nbsp; &nbsp; </a>&nbsp; </li>&nbsp; <li>&nbsp; &nbsp; <a href="#" class="kooltip" data-message="Kontakt" >&nbsp; &nbsp; &nbsp; <i class="fas fa-mail-bulk nav__link" ></i>&nbsp; &nbsp; </a>&nbsp; </li></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript