简体   繁体   English

将鼠标输入img标签时创建div

[英]Create div when mouseenter in an img tag

I want to create a div with left and top when the mouse cursor is above a img tag, I tried some things but doesn't work. 当鼠标光标在img标签上方时,我想用左和上创建一个div ,我尝试了一些操作但不起作用。 Here's my code: 这是我的代码:

$(document).on('mouseenter', 'img', function(){
     var vid = this.id;
     if(vid == "marcker"){
       $("<div/> ",{
          id: "test",
          text:"tese",
          left: mousex+"px",//this part doesn't work
          top: mousey+"px",//this part doesn't work
          heigth:"40px", //this part doesn't work
          width: "40px", //this part doesn't work
          style:"background-color:red;height:50px;",
          class: "classA"
        }).appendTo("body");
      }
});
  1. You misspelled the word "heigHT" 您拼写错误的单词“ heigHT”
  2. The width attribute isn't supported anymore so you have to use CSS. 不再支持width属性,因此您必须使用CSS。
  3. What are the mousex and mousey objects? 什么是mousex和mousey对象?

i resolved my problem 我解决了我的问题

\\function that take mouse position when the mouse point is above the tag img \\ function当鼠标指针位于标签img上方时占据鼠标位置

 $(document).on('mouseenter', 'img', function(){
                if(this.id == "marcker"){
                var x = event.pageX;
                var y = event.pageY;
                create_new_div(x,y);

             }
      });

function that create a new div with mouse position 使用鼠标位置创建新div的函数

function create_new_div(mousex,mousey){
               div = document.createElement("div");
               div.style.position = "absolute";
               div.style.left = mousex + 'px';
               div.style.top =  mousey + 'px'; 
               div.style.width = "30px";
               div.style.height = "30px";
               div.style.background = "red";
               div.style.color = "blue";

               document.body.appendChild(div);
           }

thanks alot for help 非常感谢您的帮助

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

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