gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:47:53 25 4
gpt4 key购买 nike

我与这个小问题斗争了好几天。

我需要从顶行中选择一个图像并将其添加到表格的任何选定单元格中。我设法通过点击做到了。

我的问题是如何通过拖动将相同的图像添加到单元格中。我尝试使用 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>

最佳答案

试试这个代码:

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 = "";
}
};
}
}
}
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>

关于javascript - 通过拖动将图像添加到动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com