簡體   English   中英

將鼠標輸入img標簽時創建div

[英]Create div when mouseenter in an img tag

當鼠標光標在img標簽上方時,我想用左和上創建一個div ,我嘗試了一些操作但不起作用。 這是我的代碼:

$(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. 您拼寫錯誤的單詞“ heigHT”
  2. 不再支持width屬性,因此您必須使用CSS。
  3. 什么是mousex和mousey對象?

我解決了我的問題

\\ function當鼠標指針位於標簽img上方時占據鼠標位置

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

             }
      });

使用鼠標位置創建新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);
           }

非常感謝您的幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM