拖动

2016-10-11 24 views
3

将图像添加到动态表格我正在为这个小问题奋斗了好几天。拖动

我需要从顶行选择一个图像,并将其添加到表中的任何选定单元格。我设法做到了点击。
我的问题是如何通过拖动将相同的图像添加到单元格。我尝试使用onmouseover如果onmousedown,但它根本不工作。

我不想使用jquery。

这是我的虚拟图像代码的简化版本。谢谢。

function fillup(img) { 
 
    var symbol = img.src; 
 
    var tbl = document.getElementById("New"); 
 
    for (var i = 0; i < tbl.rows.length; i++) { 
 
    for (var j = 0; j < tbl.rows[i].cells.length; j++) { 
 
     tbl.rows[i].cells[j].onclick = (function(i, j) { 
 
     return function() { 
 
      tbl.rows[i].cells[j].innerHTML = '<img src="">'; 
 
      tbl.rows[i].cells[j].innerHTML = '<img src=' + symbol + '>'; 
 
     }; 
 
     }(i, j)); 
 
    } 
 
    } 
 
}
table { 
 
    border: 1px solid black; 
 
    table-layout: fixed; 
 
    width: 180px; 
 
} 
 
td { 
 
    border: 1px solid black; 
 
    overflow: hidden; 
 
    width: 60px; 
 
    height: 60px; 
 
}
<div class="symbolToolbars" id="symbolToolbars" style="display: inline-block;"> 
 
    <div> 
 
    <a href="#" class="button"> 
 
     <img src="https://www-01.ibm.com/software/be/analytics/images/brandpages/icon-bi60x60.jpg" onclick="fillup(this)" /> 
 
    </a> 
 
    <a href="#" class="button"> 
 
     <img src="http://www.citiesofthefuture.eu/wp-content/uploads/2016/09/recycle-29227_640-60x60.png" onclick="fillup(this)" /> 
 
    </a> 
 
    <a href="#" class="button"> 
 
     <img src="https://maincdn-usedsolodresses.netdna-ssl.com/images/bookmark/large/email.jpg" onclick="fillup(this)" /> 
 
    </a> 
 

 
    </div> 
 
</div> 
 
<br /> 
 
<table border="1" width="180" height="180" id="New" style="cursor: pointer"> 
 
    <tr> 
 
    <td></td> 
 
    <td></td> 
 
    <td></td> 
 
    </tr> 
 
    <tr> 
 
    <td></td> 
 
    <td></td> 
 
    <td></td> 
 
    </tr> 
 
    <tr> 
 
    <td></td> 
 
    <td></td> 
 
    <td></td> 
 
    </tr> 
 
</table>

+0

的可能重复[使用Javascript:拖放图片标签(http://stackoverflow.com/问题/ 12036136/javascript-drag-and-drop-image-tag) –

+0

不,我不想使用jQuery。 在这一行,当我改变onclick onmouseover,它的作品,但我没有太多的控制,以填补图像的单元格。 tbl.rows [i] .cells [j] .onclick =(function(i,j){ like that tbl.rows [i] .cells [j] .onmouseover =(function(i,j){ 我需要鼠标向下,选择一个单元格,然后向下或向外插入相同的图像,同时移动。只要我做鼠标,不应该更改单元格内容应该改变。 后来,我需要选择几个填充使用不同的图像单元格,向下拖动并用同一组图像填充一些行。 – Albina

回答

0

试试这个代码:

var symbol=""; 
 
function fillup(img) { 
 
\t symbol = img.src; 
 
} 
 

 
function load(){ 
 
    var tbl = document.getElementById("New"); 
 
    for (var i = 0; i < tbl.rows.length; i++) { 
 
    for (var j = 0; j < tbl.rows[i].cells.length; j++) { 
 
     tbl.rows[i].cells[j].onmouseenter = function(){ 
 
      if(symbol!=""){ 
 
\t \t \t this.innerHTML = '<img src="">'; 
 
\t \t \t this.innerHTML = '<img src=' + symbol + '>'; 
 
\t \t \t symbol = ""; 
 
\t \t \t } 
 
\t \t }; 
 
    } 
 
    } 
 
}
table { 
 
    border: 1px solid black; 
 
    table-layout: fixed; 
 
    width: 180px; 
 
} 
 
td { 
 
    border: 1px solid black; 
 
    overflow: hidden; 
 
    width: 60px; 
 
    height: 60px; 
 
}
<body onload="load()"> 
 
    <div class="symbolToolbars" id="symbolToolbars" style="display: inline-block;"> 
 
     <div> 
 
     <a href="#" class="button"> 
 
      <img src="https://www-01.ibm.com/software/be/analytics/images/brandpages/icon-bi60x60.jpg" onmousedown="fillup(this)" /> 
 
     </a> 
 
     <a href="#" class="button"> 
 
      <img src="http://www.citiesofthefuture.eu/wp-content/uploads/2016/09/recycle-29227_640-60x60.png" onmousedown="fillup(this)" /> 
 
     </a> 
 
     <a href="#" class="button"> 
 
      <img src="https://maincdn-usedsolodresses.netdna-ssl.com/images/bookmark/large/email.jpg" onmousedown="fillup(this)" /> 
 
     </a> 
 

 
     </div> 
 
    </div> 
 
    <br /> 
 
    <table border="1" width="180" height="180" id="New" style="cursor: pointer"> 
 
     <tr> 
 
     <td></td> 
 
     <td></td> 
 
     <td></td> 
 
     </tr> 
 
     <tr> 
 
     <td></td> 
 
     <td></td> 
 
     <td></td> 
 
     </tr> 
 
     <tr> 
 
     <td></td> 
 
     <td></td> 
 
     <td></td> 
 
     </tr> 
 
    </table> 
 

 
</body>