拖放屏幕上具有相同 ID 类和标记 HTML 的多个元素的任意位置
大家好,这是我的第一个问题,所以我可能做错了.我想要实现的是拥有多个 <aside>
元素,它们都具有相同的 ID 类,当然标签可以通过拖放特性移动到屏幕上的任何位置.我找到了一个 JSFiddle,它展示了我所基于的代码,它使用一个可以移动到任何地方的侧面元素,但在使用多个元素时将不起作用.控制运动的代码在这里:
Hello everyone this is my first question so I might have done it wrong.
What I am trying to achieve is is have multiple <aside>
elements all with the same ID Class and of course Tag be able to be moved anywhere on the screen with drag and drop characteristics. I have found a JSFiddle demonstrating the code I am basing this around that uses one aside element with the ability to be moved anywhere, but will not work when multiple elements are used. The code controlling the movement is here:
function drag_start(event) {
var style = window.getComputedStyle(event.target, null);
event.dataTransfer.setData("text/plain",
(parseInt(style.getPropertyValue("left"),10) - event.clientX) + ',' +
(parseInt(style.getPropertyValue("top"),10) - event.clientY));
}
function drag_over(event) {
event.preventDefault();
return false;
}
function drop(event) {
var offset = event.dataTransfer.getData("text/plain").split(',');
var dm = document.getElementById('dragme');
dm.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
dm.style.top = (event.clientY + parseInt(offset[1],10)) + 'px';
event.preventDefault();
return false;
}
var dm = document.getElementById('dragme');
dm.addEventListener('dragstart',drag_start,false);
document.body.addEventListener('dragover',drag_over,false);
document.body.addEventListener('drop',drop,false);
我遇到的问题是放置系统,它要求元素具有单独的 ID.我还需要让所有旁边的元素都具有相同的 id,并且我不想使用类.在过去的三天里,我一直在尝试、思考和搜索网络,但没有找到答案,因为所有链接都返回到相同的代码.我已经研究过获取单击的最后一个元素的索引,但找不到获取具有相同 ID 的所有元素的方法.提前致谢 - bybb更新:已经破坏了 dnd 系统需要帮助:
The part I am having trouble with is the drop system which requires the elements to have individual ids. I also need to have all aside elements have to same id and I don't want to use classes. I have tried and thought and searched the web for the past three days with no luck of finding an answer as all link back to this same code. I have looked into getting the index of the last element clicked but cannot find a way to get all elements with the same ID. Thanks in advanced - bybb Update: Have broken the dnd system need help with that:
var dragindex = 0;
$(document).ready(function(){
$("aside").click(function(){
dragindex = $(this).index();
});
});
function drag_start(event) {
var style = window.getComputedStyle(event.target, null);
event.dataTransfer.setData("text/plain",
(parseInt(style.getPropertyValue("left"),10) - event.clientX) + ',' + (parseInt(style.getPropertyValue("top"),10) - event.clientY));
}
function drag_over(event) {
event.preventDefault();
return false;
}
function drop(event) {
var offset = event.dataTransfer.getData("text/plain").split(',');
var dm = document.getElementById('#winborder');
dm.style.left = (event.clientX + parseInt(offset[0],10)) + 'px';
dm.style.top = (event.clientY + parseInt(offset[1],10)) + 'px';
event.preventDefault();
return false;
}
function makewindow(content, storevar) {
storevar = document.createElement('aside');
storevar.setAttribute("dragable", "true");
storevar.setAttribute("id", "winborder");
var content = document.createElement('div');
content.setAttribute("id", "wincontent");
storevar.appendChild(content);
document.body.appendChild(storevar);
storevar.addEventListener('dragstart',drag_start,false);
document.body.addEventListener('dragover',drag_over,false);
document.body.addEventListener('drop',drop,false);
}
尝试过在 getelementbyid 上使用和不使用 '#'
Have tried with and without '#' on getelementbyid
推荐答案
以下代码将允许拖放;很抱歉它没有遵循您以前的编码风格.
The following code will allow drag and drop; I'm sorry it doesn't follow your previous coding style.
这是一个展示它的 JSFiddle:https://jsfiddle.net/6gq7u8Lc/
Here is a JSFiddle showing it in action: https://jsfiddle.net/6gq7u8Lc/
document.getElementById("dragme").onmousedown = function(e) {
this.prevX = e.clientX;
this.prevY = e.clientY;
this.mouseDown = true;
}
document.getElementById("dragme").onmousemove = function(e) {
if(this.mouseDown) {
this.style.left = (Number(this.style.left.substring(0, this.style.left.length-2)) + (e.clientX - this.prevX)) + "px";
this.style.top = (Number(this.style.top.substring(0, this.style.top.length-2)) + (e.clientY - this.prevY)) + "px";
}
this.prevX = e.clientX;
this.prevY = e.clientY;
}
document.getElementById("dragme").onmouseup = function() {
this.mouseDown = false;
}
附带说明,您将无法为他们提供所有相同的 ID.DOM 不支持这样的事情.如果您想添加多个相同类型的元素,我建议使用容器"父 div,将元素添加为 div 的子元素,并遍历 .children 属性来访问每个元素.
On a side note, you won't be able to give them all the same id. The DOM doesn't support such a thing. If you want to add multiple elements of the same type, I would suggest a 'container' parent div, adding the elements as children of the div, and iterating through the .children attribute to access each.
相关文章