XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!doctype html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Drag</title>
- <style>
- .box{
- width: 400px;
- height: 400px;
- float: left;
- }
- #box1{
- background: #CCC;
- }
- #box2{
- background: #FF0;
- }
- </style>
- </head>
- <body>
- <div id="box1" class="box"></div>
- <div id="box2" class="box"></div>
- <img src="http://pica.zol-img.com.cn/2016/02/1ace90ad77db716547614a18c4a9263g.jpg" alt="" id="img1" />
JavaScript Code復(fù)制內(nèi)容到剪貼板
- <script src="app1.js"></script>
- </body>
- </html>
-
- app1.js
-
-
-
-
- var oBox1,
- oBox2,
- oImg1;
-
- window.onload = function(){
- oBox1 = document.getElementById('box1');
- oBox2 = document.getElementById('box2');
- oImg1 = document.getElementById('img1');
-
-
- oBox1.ondragover = oBox2.ondragover = function(e){
- e.preventDefault();
- };
-
-
- oImg1.ondragstart = function(e){
- e.dataTransfer.setData('text', e.target.id);
- };
-
- oBox1.ondrop = dropImg;
- oBox2.ondrop = dropImg;
- };
-
- function dropImg(e){
- e.preventDefault();
- var tempImg = document.getElementById(e.dataTransfer.getData('text'));
- e.target.appendChild(tempImg);
- }
W3Cschool上的解釋是:返回觸發(fā)此事件的元素(事件的目標(biāo)節(jié)點(diǎn)),這個(gè)target屬性只兼容ie9及以上