h5教程分享DOM中的表单操作,节点操作

 所属分类:web前端开发

 浏览:130次-  评论: 0次-  更新时间:2022-10-27
描述:更多教程资料进入php教程获得。 前端(vue)入门到精通课程:进入学习Apipost = Postman + Swagger + Mock + Jmeter 超好用的API...
更多教程资料进入php教程获得。

前端(vue)入门到精通课程:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
window.onload = function(){
var input = document.querySelectorAll('input');
var table = document.querySelector('table');
var rows = table.tBodies[0].rows;
var arrRows = [];
/* 因为只能对数组排序,所以把tr都放入数组 */
for(var i = 0; i < rows.length; i++){
arrRows.push(rows[i]);
}
// arrRows.sort(function(a,b){
// return b.cells[1].innerHTML - a.cells[1].innerHTML;
// //根据tr中的第1个单元格中的内容进行排序
// });
// arrRows.forEach(function(tr){
// //按照排号的顺序,重新把tr放入tbody
// table.tBodies[0].appendChild(tr);
// });
//console.log(arrRows);
input[0].onclick = function(){
arrRows.sort(function(a,b){
return b.cells[1].innerHTML - a.cells[1].innerHTML;
//根据tr中的第1个单元格中的内容进行排序
});
arrRows.forEach(function(tr){
//按照排号的顺序,重新把tr放入tbody
table.tBodies[0].appendChild(tr);
});
}
input[1].onclick = function(){
arrRows.sort(function(a,b){
return a.cells[1].innerHTML - b.cells[1].innerHTML;
//根据tr中的第1个单元格中的内容进行排序
});
arrRows.forEach(function(tr){
//按照排号的顺序,重新把tr放入tbody
table.tBodies[0].appendChild(tr);
});
}
};
</script>
</head>
<body>
<table border="1" width="400" align="center">
<thead>
<tr>
<th>水果</th>
<th>单价(¥)</th>
</tr>
</thead>
<tbody>
<tr>
<th>苹果</th>
<th>54.5</th>
</tr>
<tr>
<th>橘子</th>
<th>24.5</th>
</tr>
<tr>
<th>西瓜</th>
<th>33.8</th>
</tr>
<tr>
<th>香蕉</th>
<th>13.8</th>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2" style="text-align: center;">
<input type="button" value="价格从高到低排列">
<input type="button" value="价格从低到高排列">
</td>
</tr>
</tfoot>
</table>
</body>
</html>

以上就是分享DOM中的表单操作,节点操作的详细内容,更多请关注zzsucai.com其它相关文章!

积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!