我有一个div内的3个图像。我如何动态加载和覆盖在这三个图像在div中的一个图像?加载另一个图像
我的HTML看起来像这样
<div id="div1">
<img id="img1" src="images/Green.png" draggable="false" class="tile" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="img2" src="images/Green.png" draggable="false" class="tile" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="img3" src="images/Green.png" draggable="false" class="tile" ondrop="drop(event)" ondragover="allowDrop(event)">
</div>
<div id="inner">
<img id="dragRed" src="images/Red.png" draggable="true" ondragstart="drag(event)" width="5%" height="5%">
<img id="dragYellow" src="images/Yellow.png" draggable="true" ondragstart="drag(event)" width="5%" height="5%">
<img id="dragGreen" src="images/Green.png" draggable="true" ondragstart="drag(event)" width="5%" height="5%">
</div>
我使用这个脚本尝试,但它会导致IMG1消失。
$("#img1").each(function() {
var $overlay = $('<div></div>');
$overlay.css({
position: "relative",
display: "inline-block",
width: $(this).width(),
height: $(this).height(),
backgroundPosition: "center center",
backgroundImage: "url(images/affection.png)"
});
$(this).wrap($overlay);
});
'$(“#img1”)。each'将查找'id'为'img1'的多个元素。 “身份证”必须是唯一的。由于所有的图像都有一个'tile'类,你可以通过改变这段代码来读取'$(“。tile”)。' – Yass
我实际上只想改变img1 – Jetrica