通过拖动将图像添加到动态表格

时间:2016-10-11 01:38:19

标签: javascript

我正在和这个小问题斗争好几天。

我需要从顶行选择一个图像并将其添加到表格的任何选定单元格中。我设法点击了。
我的问题是如何通过拖动将相同的图像添加到单元格。我尝试使用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));
    }
  }
}
&#13;
table {
  border: 1px solid black;
  table-layout: fixed;
  width: 180px;
}
td {
  border: 1px solid black;
  overflow: hidden;
  width: 60px;
  height: 60px;
}
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

试试这段代码:

&#13;
&#13;
var symbol="";
function fillup(img) {
	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!=""){
			this.innerHTML = '<img src="">';
			this.innerHTML = '<img src=' + symbol + '>';
			symbol = "";
			}
		};
    }
  }
}
&#13;
table {
  border: 1px solid black;
  table-layout: fixed;
  width: 180px;
}
td {
  border: 1px solid black;
  overflow: hidden;
  width: 60px;
  height: 60px;
}
&#13;
<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>
&#13;
&#13;
&#13;