如何使用JavaScript函数在超链接上添加动态工具提示

huangapple go评论59阅读模式
英文:

How to add a dynamic tooltip over a hyperlink using a function on JS

问题

I have a link, and I want to display a description in a tooltip when hovering. I've been using setAttribute, but it does not work:

let link = document.createElement("a");
link.setAttribute("href", `http://mylink.co/${element.key}`);
link.setAttribute("target", "_blank");
link.setAttribute("onmouseover", element.description); // <----- here
link.innerHTML = element.key + ": " + element.summary;

I know this shouldn't be this complex but I'm not very familiarized with JavaScript.

I also tried adding it as a function:

link.onmouseover = function() { element.description) } 
英文:

I have a link, and I want to display a description in a tooltip when hovering. I've been using setAttribute, but it does not work:

    let link = document.createElement(&quot;a&quot;);
    link.setAttribute(&quot;href&quot;, `http://mylink.co/${element.key}`);
    link.setAttribute(&quot;target&quot;, &quot;_blank&quot;);
    link.setAttribute(&quot;onmouseover&quot;, element.description); // &lt;----- here
    link.innerHTML = element.key + &quot;: &quot; + element.summary;

I know this shouldn't be this complex but I'm not very familiarized with JavaScript.

I also tried adding it as function:

link.onmouseover = function() { element.description) } 

答案1

得分: 1

你只需要添加这部分:

link.setAttribute("title", "在这里添加你的描述");

英文:

that you need is to add this:

link.setAttribute(&quot;title&quot;, &quot;your description here&quot;);

huangapple
  • 本文由 发表于 2023年3月4日 00:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定