问题
I like to have a list which elements can be dropped in a list in an iframe similar to this example. I found a working solution via this thread but in my case I need other elements than <li>
's.
Here's my setup:
<div id="container">
<ul>
<li>To drop 1</li>
<li>To drop 2</li>
</ul>
</div>
<iframe id="frame" src=""></iframe>
and the iframe content:
<ul id="sortable">
<li>Element</li>
<li>Element</li>
<li>Element</li>
</ul>
This works as you can see here.
It even works when changing the <ul>
and <li>
's to <div>
(example and iframe).
When I use custom tags it's working with <foo>
and <bar>
tags but what I really need is to get it working with <modules>
and <module>
tags.
<foo id="sortable"> //WORKS!!
<bar>Element</bar>
<bar>Element</bar>
<bar>Element</bar>
</foo>
<modules id="sortable"> //DOESN'T WORK!!
<module>Element</module>
<module>Element</module>
<module>Element</module>
</modules>
Here's the fiddle and the iframe of what I actually need to work.
So basically the draggable and sortable method is not working with my custom html tags. What's so different between <foo>
, <bar>
and <modules>
, <module>
?
UPDATE
It seems this is an issue on webkit browsers only since it's working fine on FF - wasn't able to test on a Windows machine yet.
When dragging the element it will create a "helper" which gets some inline style:
position:absolute;left:XXXpx;right:XXXpx;width:XXXpx;heightXXXpx;z-index:1000
while on webkits it only get position
, left
and right
:
position:absolute;left:XXXpx;right:XXXpx;
回答1:
Try using HTML5 Drag and Drop
<script id="dnd">
function over(e) {
e.preventDefault();
}
function drag(e) {
e.dataTransfer.setData("text", e.target.dataset.id);
}
function drop(e) {
e.preventDefault();
var data = e.dataTransfer.getData("text");
e.target.appendChild(
parent.document.querySelector("[data-id='" + data + "']")
);
}
window.onload = function() {
var mods = document.querySelector("modules");
var script = document.getElementById("dnd");
var iframe = document.querySelector("iframe");
iframe.src = URL.createObjectURL(
new Blob(
["<!doctype html>" + script.outerHTML + mods.outerHTML]
, {"type": "text/html"}
)
);
}
</script>
<div id="container">
<ul>
<li data-id="1" ondragstart="drag(event)" draggable="true">To drop 1</li>
<li data-id="2" ondragstart="drag(event)" draggable="true">To drop 2</li>
</ul>
</div>
<modules ondrop="drop(event)" ondragover="over(event)">
<module>Element</module>
<module>Element</module>
<module>Element</module>
<style>
module, li {
border:1px solid #000;
display:block;
list-style:none;
margin:0;
padding:5px;
}
</style>
</modules>
<iframe src=""></iframe>
jsfiddle http://jsfiddle.net/zo2bx4f7/
回答2:
The same issue appears with Sortable.
I found that its because of the length of the tag. It must not be longer than 4 chars total.
Check out this fiddle:
Sortable.create(document.getElementById('sortable-1'), {});
Sortable.create(document.getElementById('sortable-2'), {});
Sortable.create(document.getElementById('sortable-3'), {});
Sortable.create(document.getElementById('sortable-4'), {});
foo,
modu, modul, modules {
display: block;
}
foo > bar,
modu > module,
modul > module,
modules > module {
display: block;
padding: 10px;
border: 1px solid blue;
margin: 5px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.4.2/Sortable.min.js"></script>
<foo id="sortable-1">
<bar>foo 1</bar>
<bar>foo 2</bar>
<bar>foo 3</bar>
</foo>
<br/><br/>
<modu id="sortable-2">
<module>modu 1</module>
<module>modu 2</module>
<module>modu 3</module>
</modu>
<br/><br/>
<modul id="sortable-3">
<module>modul 1</module>
<module>modul 2</module>
<module>modul 3</module>
</modul>
<br/><br/>
<modules id="sortable-4">
<module>modules 1</module>
<module>modules 2</module>
<module>modules 3</module>
</modules>
来源:https://stackoverflow.com/questions/33214810/make-custom-html-elements-droppable-in-iframe