我正在尝试创建一个程序,在该程序中,您首先要点击“Step”按钮,然后左上角单元格内的
随着“Step”按钮的所有后续点击,该图像沿表格的周边单元格顺时针移动一个单元格。当该图像移出单元格时,应恢复该单元格的原始文本。点击“Reset”按钮应将页面恢复到其初始状态。
注意:或者,您可以仅将
非常感谢您的帮助!
这里是一个JSfiddle,其中包含注释代码,以解释我为什么要这样做。
<p>
元素应替换为<img>
元素,即您选择的<img>
元素。随着“Step”按钮的所有后续点击,该图像沿表格的周边单元格顺时针移动一个单元格。当该图像移出单元格时,应恢复该单元格的原始文本。点击“Reset”按钮应将页面恢复到其初始状态。
注意:或者,您可以仅将
<p>
添加到表格的周边单元格,并直接在不在表格周边的<td>
元素中编写文本。非常感谢您的帮助!
这里是一个JSfiddle,其中包含注释代码,以解释我为什么要这样做。
function moveImageAlongCells() { //Function to move the image along the cells
var table = document.getElementById('myTable');
reset(table);
var img = document.createElement("IMG"); //create the image
img.setAttribute("src", "img_pulpit.jpg"); //Example from internet
for (var r = 0, n = table.rows.length; r < n; r++) {
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
// alert(table.rows[r].cells[c].innerHTML); // This worked as it went across all the the cells
temp = table.rows[r].cells[c];
table.rows[r].cells[c].removechild("P"); //Do I remove the <P> element?
table.rows[r].celss[c].appendChild("img"); // And then add the <img> element
}
}
}
function reset(old) {
document.getElement(old); //Trying to save the table without edits so that the reset button works
}
table, td {
border: 1px solid black;
}
<p>Click the Step button and it will move the image over to the next cell all the way along the PERIMETER of the cell. The reset button then will reset the table back to normal. With no images</p>
<table id="myTable">
<tr>
<td>
<p>Row1 cell1</p>
</td>
<td>
<p>Row1 cell2</p>
</td>
<td>
<p>Row1 cell3</p>
</td>
<td>
<p>Row1 cell4</p>
</td>
</tr>
<tr>
<td>
<p>Row2 cell1</p>
</td>
<td>
<p>Row2 cell2</p>
</td>
<td>
<p>Row2 cell3</p>
</td>
<td>
<p>Row2 cell4</p>
</td>
</tr>
<tr>
<td>
<p>Row3 cell1</p>
</td>
<td>
<p>Row3 cell2</p>
</td>
<td>
<p>Row3 cell3</p>
</td>
<td>
<p>Row3 cell4</p>
</td>
</tr>
<tr>
<td>
<p>Row4 cell1</p>
</td>
<td>
<p>Row4 cell2</p>
</td>
<td>
<p>Row4 cell3</p>
</td>
<td>
<p>Row4 cell4</p>
</td>
</tr>
</table>
<br>
<!-- calls the function that moves the image -->
<button onclick="moveImageAlongCells()">STEP</button>
<!-- resets the table to it's original form. (without any images) -->
<button onclick="reset()">RESET</button>