我有一個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