基本上,我是一名刚开始使用Javascript的初学者,我想简化我的代码。我该如何简化这个?我如何应用循环,因为如果我想更改id
,则单个代码声明它太长。不是在html中手动更改,而是使用Javascript来更改html中的值?
<table border="2" cellpadding="4">
<tbody>
<tr>
<td id="cell1"> one </td>
<td id="cell2"> two </td>
<td id="cell3"> three </td>
</tr>
<tr>
<td id="cell4"> four </td>
<td id="cell5"> five </td>
<td id="cell6"> six </td>
</tr>
</tbody>
</table>
document.getElementById("cell1").innerHTML="1";
document.getElementById("cell2").innerHTML="2";
document.getElementById("cell3").innerHTML="3";
document.getElementById("cell4").innerHTML="4";
document.getElementById("cell5").innerHTML="5";
document.getElementById("cell6").innerHTML="6";
答案 0 :(得分:1)
使用New-Item 20171206 -ItemType Directory | Set-Location
for loop
var items = document.getElementsByTagName('td');
for (var i = 0, j = 1; i <= items.length - 1; i++, j++) {
items[i].innerHTML = j;
}
答案 1 :(得分:-1)
我希望帮助您和您开始在此网站https://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd上学习javascript非常有用:
var d = document;
var trs = d.getElementsByTagName("tr");
console.log(trs);
var count = 1;
for(var i=0 ; i<trs.length ; i++){
tds = trs[i].getElementsByTagName("td");
for(var j=0 ; j<tds.length ; j++){
tds[j].innerHTML = count;
count++;
}
console.log(trs[i]);
}