Javascript 如何使用CSS和js为div中的每个图像创建关闭按钮

Javascript 如何使用CSS和js为div中的每个图像创建关闭按钮,javascript,jquery,css,html5-canvas,Javascript,Jquery,Css,Html5 Canvas,如何为悬停的每个图像创建关闭按钮?有一个div,其中每个图像都可以拖动 我需要在每个图像附近做一个关闭按钮。那个按钮 只有当我们在图像上触摸或移动鼠标指针时才需要可见,这就是按钮需要显示的悬停效果 这是我的HTML $(函数(){ var a=1; $(“.child”).draggable({ 遏制:“家长”, start:function(event,ui){$(this.css(“z-index”,a++);} }); } ); 用单独的div包装每个图像,将.child类添加到该div

如何为悬停的每个图像创建关闭按钮?有一个div,其中每个图像都可以拖动

我需要在每个图像附近做一个关闭按钮。那个按钮 只有当我们在图像上触摸或移动鼠标指针时才需要可见,这就是按钮需要显示的悬停效果

这是我的HTML

$(函数(){
var a=1;
$(“.child”).draggable({
遏制:“家长”,
start:function(event,ui){$(this.css(“z-index”,a++);}
});
} );

用单独的
div
包装每个图像,将
.child
类添加到该
div
而不是图像:

<div class="parent">
  <div class='child'>
    <img src ='https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Circle_Red.png' />
  </div>
  <div class='child'>
    <img src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Wand-128.png' />
  </div>
  <div class='child'>
    <img src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Candy-01-128.png' />
  </div>
</div>

然后:

var a = 1;
$( ".child" ).draggable({
  containment: "parent",
  start: function(event, ui) { $(this).css("z-index", a++); }
}).hover(function(){
  $(this).prepend('<button class="remove">x</button>');
}, function(){
  $(this).find('.remove').remove();
}).on('click', '.remove', function(){
  $(this).closest('.child').remove();
});
var a=1;
$(“.child”).draggable({
遏制:“家长”,
start:function(event,ui){$(this.css(“z-index”,a++);}
}).hover(函数(){
$(this.prepend('x');
},函数(){
$(this.find('.remove').remove();
}).on('click','remove',函数(){
$(this).closest('.child').remove();
});

用单独的
div
包装每个图像,将
.child
类添加到该
div
中,而不是图像:

<div class="parent">
  <div class='child'>
    <img src ='https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Circle_Red.png' />
  </div>
  <div class='child'>
    <img src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Wand-128.png' />
  </div>
  <div class='child'>
    <img src ='https://cdn2.iconfinder.com/data/icons/free-color-halloween-icons/24/Candy-01-128.png' />
  </div>
</div>

然后:

var a = 1;
$( ".child" ).draggable({
  containment: "parent",
  start: function(event, ui) { $(this).css("z-index", a++); }
}).hover(function(){
  $(this).prepend('<button class="remove">x</button>');
}, function(){
  $(this).find('.remove').remove();
}).on('click', '.remove', function(){
  $(this).closest('.child').remove();
});
var a=1;
$(“.child”).draggable({
遏制:“家长”,
start:function(event,ui){$(this.css(“z-index”,a++);}
}).hover(函数(){
$(this.prepend('x');
},函数(){
$(this.find('.remove').remove();
}).on('click','remove',函数(){
$(this).closest('.child').remove();
});

非常感谢你的朋友。我对这个问题还有一个疑问。我将把它作为另一个问题和过去,但由于某些原因,我不能用单独的div包装每个图像。如何解决这种情况?这是现实生活中的情况还是只是一个示例场景?没有什么是不可能的,但这将需要一些肮脏的黑客…请看这个问题。这是我的疑问。我把它作为一个单独的问题,这样每个其他用户都能理解如何做。真的谢谢你的朋友。我对这个问题还有一个疑问。我将把它作为另一个问题和过去,但由于某些原因,我不能用单独的div包装每个图像。如何解决这种情况?这是现实生活中的情况还是只是一个示例场景?没有什么是不可能的,但这将需要一些肮脏的黑客…请看这个问题。这是我的疑问。我把它作为一个单独的问题,以便其他用户都能理解如何做到这一点。