gpt4 book ai didi

javascript - 使用某种 ID 跟踪拖放图像

转载 作者:行者123 更新时间:2023-11-28 12:17:06 25 4
gpt4 key购买 nike

好的,我在为电子商务网站制作东西时遇到了一个问题。

基本上,这是一个屏幕,用户可以在其中将小按钮拖动到他们在上一个屏幕上选择的手镯上。他们将按钮拖到手镯上,一旦他们喜欢这个组合,他们应该能够一次购买所有 4 件商品(1 个手镯,3 个按钮)。

这是它基本工作原理的 jsFiddle:http://jsfiddle.net/6xRXj/3/

当我需要知道手镯上有哪些按钮时,问题就来了。有没有办法通过拖放来做到这一点?

非常感谢任何帮助。

下面是来自 jsFiddle 的代码...

HTML:

<div id="bracelet">
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<div id="div2" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag1" src="http://upload.wikimedia.org/wikipedia/commons/4/4f/Button-Red.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div3" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag2" src="http://upload.wikimedia.org/wikipedia/commons/a/a8/Button-Blue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div4" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag3" src="http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div5" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag4" src="http://upload.wikimedia.org/wikipedia/commons/b/ba/Button-Purple.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div6" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag5" src="http://upload.wikimedia.org/wikipedia/commons/6/68/Button-Orange.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div7" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag6" src="http://upload.wikimedia.org/wikipedia/commons/d/dc/Button-Green.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div8" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag7" src="http://upload.wikimedia.org/wikipedia/commons/4/4f/Button-Red.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div9" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag8" src="http://upload.wikimedia.org/wikipedia/commons/a/a8/Button-Blue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div10" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag9" src="http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div11" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag10" src="http://upload.wikimedia.org/wikipedia/commons/b/ba/Button-Purple.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div12" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag11" src="http://upload.wikimedia.org/wikipedia/commons/6/68/Button-Orange.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div13" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag12" src="http://upload.wikimedia.org/wikipedia/commons/d/dc/Button-Green.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

CSS:

#div1 {
width:50px;
height:50px;
padding:10px;
border:1px solid #aaaaaa;
display: inline;
float: left;
margin-bottom: 25px;
margin-left: 50px
}
.drop {
width:50px;
height:50px;
padding:10px;
border:1px solid #aaaaaa;
display: inline;
float: left;
}
#bracelet {
width:420px;
height:75px;
padding: 10px;
margin-left: 30px;
border:1px solid #aaaaaa;
background-color: gray;
}

JS:

function allowDrop(ev) {
ev.preventDefault();
}

function drag(ev) {
var el = ev.target;
var parent = el.getAttribute("data-parent");

if(!parent){
el.setAttribute("data-parent", el.parentNode.id);
}

ev.dataTransfer.setData("Text", el.id);
}

function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}

function dragEnd(ev){
if(ev.dataTransfer.dropEffect == "none"){
var parent = document.getElementById(ev.target.getAttribute("data-parent"));

parent.appendChild(ev.target);
}
}

最佳答案

那么我们需要修改您的 drop 函数的某些部分。我已经声明了一个名为 selectedItemsarray。当按钮从可用元素拖到 bracelet div 时,我们将其放入数组中。如果它没有被放到 bracelet 上,我们就将它从我们的数组中移除。您可以随时单击获取按钮获取所选元素。

////array to hold selected buttons
var selectedItems = [];

function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("Text"),
item = document.getElementById(data),
i = 0,
elem = document.getElementById(data);

ev.target.appendChild(item);

////MODIFICATION
if (ev.target.parentNode === document.getElementById('bracelet')) {
selectedItems.push(elem);
} else {
i = selectedItems.indexOf(elem);
selectedItems.splice(i, 1);
}
}

阅读选定的元素:

function getGems() {
var pra = document.getElementById("ptag"),
i = 0;
pra.innerText ='';
try {
for (i = 0; i < selectedItems.length; i++) {
if (selectedItems[i]) {
pra.innerText += ", " + selectedItems[i].id
}
}
} catch (er) {
pra.innerText = 'Error: ' + er;
}
}

更新 fiddle :http://jsfiddle.net/amantur/6xRXj/5/

关于javascript - 使用某种 ID 跟踪拖放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19106183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com