简体   繁体   English

我想按事件显示 HTML 元素

[英]I would like to show HTML element by event

I'm trying to make a tooltip by clicking a shape.我正在尝试通过单击形状来制作工具提示。 I've done with adding event listener to canvas shape.我已经将事件监听器添加到 canvas 形状。 However, I could not add tooltip such as HTML element.但是,我无法添加诸如 HTML 元素之类的工具提示。

I tried to implement tooltip by using css display:none, block我尝试使用 css display:none, block 来实现工具提示

var toolTip = document.createElement('div');
toolTip.style.width = "50px";
toolTip.style.height = "auto";
toolTip.style.padding = "5px";
toolTip.style.border = "1px solid rgb(177, 177, 177)";
toolTip.style.position = "absolute";
toolTip.style.display = "none";
toolTip.style.display = "background: rgba(111,231,43,0.5)";
toolTip.style.zIndex = "10";

canvas.addEventListener("click", e => {
      let rect = canvas.getBoundingClientRect();
      this.mousePos = {
        x: e.clientX - rect.left,
        y: e.clientY - rect.top
      };
      console.log(this.mousePos.x, this.mousePos.y);

      if(0 <= this.mousePos.x <300 && 100 <= this.mousePos.y <110) {
        console.log("mouseClicked");
        toolTip.style.display = "block";
        toolTip.style.left = this.mousePos.x + this.mousePos.x * 0.02;
        toolTip.style.top = this.mousePos.y + this.mousePos.y * 0.02;
        toolTip.innerHTML = "<p> hello </p>";
      }
});

https://codepen.io/chikichaka/pen/NWRjWyG https://codepen.io/chikichaka/pen/NWRjWyG

You create a div element to act as a tooltip but you never append the tooltip to a container.您创建一个 div 元素来充当工具提示,但您永远不会将工具提示 append 到容器中。 You must append the div to a container, such as the body你必须把 append 的 div 放到一个容器中,比如 body

document.body.appendChild(toolTip);

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

相关问题 显示html元素onclick事件 - show html element onclick event 我现在将鼠标悬停在元素上,一旦鼠标移出,我想将其隐藏。 我该怎么做呢? - I have an element show on mouseover now I would like to hide it once I mouse out. How do I do this? 我有一个选项表单,我想为我选择的选项标签的每个元素显示来自 JSON 文件的不同图像 - I have an option form and I would like to show a different image from a JSON file for each element of the option tag i choose 在HTML中呈现元素时,可以使用JavaScript设置元素的内联样式吗? 我想根据上一个div定义定位 - Can I use JavaScript to set inline style for an element as I am rendering it in the HTML? I would like to define positioning based on a previous div 我有JSON数据,我想在Full Calender上显示 - I have JSON data and I would like to show that on Full Calender 我想在 event.target.value 上检索 object 的值 - I would like to retrieve the value of the object on event.target.value 我希望我的代码只显示 2 位小数 - I would like my code to only show 2 decimal places 我想使用循环将元素向左移动 - I would like to move an element to the left using loop 我想将鼠标悬停在一个元素上以影响其他元素 - i would like hover one element to affect other elements css 我想将ios Web视图的内容导出为html - I would like to export the contents of ios web view as html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM