我使用javascript代码在我的div之间拖放。原生HTML5拖放。这段代码在chrome和firefox中工作得很好但是它不起作用IE11在控制台中也没有显示任何内容我无法找到问题
这是我的代码
<html>
<head>
<style>
[draggable] {
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
}
.box {
height: 125px;
width: 125px;
float: left;
border: 3px solid #0092BF;
background-color: #FFEBDD;
margin-right: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
text-align: center;
cursor: move;
}
.box header {
color: #fff;
text-shadow: #000 0 1px;
box-shadow: 5px;
padding: 5px;
background: -moz-linear-gradient(left center, rgb(0,0,0), rgb(79,79,79), rgb(21,21,21));
background: -webkit-gradient(linear, left top, right top,
color-stop(0, rgb(0,0,0)),
color-stop(0.50, rgb(79,79,79)),
color-stop(1, rgb(21,21,21)));
border-bottom: 1px solid #ddd;
-webkit-border-top-left-radius: 5px;
-moz-border-radius-topleft: 5px;
border-top-left-radius: 5px;
-webkit-border-top-right-radius: 5px;
-moz-border-radius-topright: 5px;
border-top-right-radius: 5px;
}
</style>
<body>
<div id="boxes-example">
<div class="box" draggable="true">
<header>A</header>
<p>
order!
</p>
</div>
<div class="box" draggable="true">
<header>B</header>
<p>
Put me
</p>
</div>
<div class="box" draggable="true">
<header>C</header>
<p>
right
</p>
</div>
<div class="box" draggable="true">
<header>D</header>
<p>
into
</p>
</div>
<div class="box" draggable="true">
<header>E</header>
<p>
the
</p>
</div>
</div>
拖放脚本:
<script>
(function () {
var id_ = 'boxes-example';
var boxes_ = document.querySelectorAll('#' + id_ + ' .box');
var dragSrcEl_ = null;
this.handleDragStart = function (e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text', this.innerHTML);
dragSrcEl_ = this;
this.style.opacity = '0.5';
// this/e.target is the source node.
this.addClassName('moving');
};
this.handleDragOver = function (e) {
if (e.preventDefault) {
e.preventDefault(); // Allows us to drop.
}
e.dataTransfer.dropEffect = 'move';
return false;
};
this.handleDragEnter = function (e) {
this.addClassName('over');
};
this.handleDragLeave = function (e) {
// this/e.target is previous target element.
this.removeClassName('over');
};
this.handleDrop = function (e) {
// this/e.target is current target element.
if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}
// Don't do anything if we're dropping on the same box we're dragging.
if (dragSrcEl_ != this) {
dragSrcEl_.innerHTML = this.innerHTML;
this.innerHTML = e.dataTransfer.getData('text');
}
return false;
};
this.handleDragEnd = function (e) {
// this/e.target is the source node.
this.style.opacity = '1';
[ ].forEach.call(boxes_, function (box) {
box.removeClassName('over');
box.removeClassName('moving');
});
};
[ ].forEach.call(boxes_, function (box) {
box.setAttribute('draggable', 'true'); // Enable boxes to be draggable.
box.addEventListener('dragstart', this.handleDragStart, false);
box.addEventListener('dragenter', this.handleDragEnter, false);
box.addEventListener('dragover', this.handleDragOver, false);
box.addEventListener('dragleave', this.handleDragLeave, false);
box.addEventListener('drop', this.handleDrop, false);
box.addEventListener('dragend', this.handleDragEnd, false);
});
})();
</script>
</body>
</html>
首先你使用'addClassName'
,AFAIK可以通过Prototype.js获得(不要把它与Jquery的addClass()
混淆,它做同样的事情)
因此,您需要包含prototype.js
或使用其他语法的另一个库
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script
>
这足以使您的脚本运行没有错误
要进行拖放操作,您需要在每个qazxsw poi和qazxsw poi事件处理程序中添加qazxsw poi:
preventDefault()
对于那些正在寻找在IE11中运行的解决方案的人来说,这是一篇很好的解释它的文章。 dragenter