简体   繁体   English

无法在HTML DOM中生成工具提示? (javascript)

[英]Cannot generate a tooltip in HTML DOM? (javascript)

I would like to generate tooltips for my list elements using Javascript (using HTML DOM elements). 我想使用Javascript(使用HTML DOM元素)为列表元素生成工具提示。 What is the easiest way to do this? 最简单的方法是什么? I am trying to generate a list of Hyperlinks, but when you mouse over each Hyperlink a tooltip pops up. 我正在尝试生成超链接列表,但是当您将鼠标悬停在每个超链接上时,会弹出一个工具提示。

function print(string){
// Create a <li> element
  var node = document.createElement("LI");         

// Create an anchor element (hyperlink)
  var anchor = document.createElement("A");
  anchor.text = string;
  anchor.href = getLinkFromCert(string);


//I WANT TO GENERATE A TOOLTIP EITHER FOR MY ANCHOR ELEMENT OR FOR MY <LI> ELEMENT



// Append the anchor to <li>  
 node.appendChild(anchor);      

 // Append <li> to <ul> with id="myList"
 document.getElementById("myList").appendChild(node);    

}

Below is the resulting HTML generated with my js. 下面是用我的js生成的结果HTML。 The elements are the actual tooltips for the anchor elemements. 元素是锚点元素的实际工具提示。 But they are not showing up. 但是他们没有露面。

Here is the tooltip library I am trying to use: https://getmdl.io/components/index.html#tooltips-section 这是我尝试使用的工具提示库: https : //getmdl.io/components/index.html#tooltips-section

If you want the default tooltips readable by screen readers, 如果您希望屏幕阅读器可以读取默认的工具提示,

anchor.title="This will popup";

or 要么

node.title="This will be an LI tooltip"

Example. 例。 Mouse over the bullet and mouse over the link 将鼠标悬停在项目符号上,并将鼠标悬停在链接上

 function getLinkFromCert() {} function print(string) { // Create a <li> element var node = document.createElement("LI"); node.title="LI title:"+string; // Create an anchor element (hyperlink) var anchor = document.createElement("A"); anchor.text = string; anchor.href = getLinkFromCert(string); anchor.title="Anchor title:"+string; // Append the anchor to <li> node.appendChild(anchor); // Append <li> to <ul> with id="myList" document.getElementById("myList").appendChild(node); } window.onload = function() { print("tooltip"); } 
 <ul id="myList"></ul> 

You should consider using a script instead of writing tooltips yourself. 您应该考虑使用脚本而不是自己编写工具提示。 I recommend tipsy, which you find here . 我建议您在这里找到提示。

If you are using Boostrap, then there are also tooltips already integrated. 如果您使用的是Boostrap,则还集成了工具提示。 You find the description here . 您可以在此处找到说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM