[英]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");
}
});
我解決了我的問題
\\ 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.