function qiehuan(){
//获取table
var table=document.getElementsByTagName("table")[0];
//获取tr
var row1=table.rows[0];
var row2=table.rows[1];
//交换内容
//创建新元素来存储数据
var a=row1.cells[0].innerHTML;
var b=row2.cells[0].innerHTML;
row1.cells[0].innerHTML=b;
row2.cells[0].innerHTML=a;
}
Document
点击替换内容 |
id |
name |
sex |
1 |
a |
男 |
2 |
b |
女 |
<script><br />
//绑定效果---ie下失效<br />
document.getElementById('id').addEventListener('click', f_switch, false);<br />
document.getElementById('name').addEventListener('click', f_switch, false);<br />
document.getElementById('sex').addEventListener('click', f_switch, false);<br />
function f_switch(){<br />
//获取table<br />
var table=document.getElementsByTagName("table")[0];<br />
//获取行元素<br />
var row1=table.rows[2];<br />
var row2=table.rows[3];<br />
//方法一<br />
//创建新元素来存储数据<br />
var newrow=document.createElement("tr");<br />
var newhtml=newrow.innerHTML=row2.innerHTML;<br />
var newrow2=document.createElement("tr");<br />
var newhtml2=newrow2.innerHTML=row1.innerHTML;<br />
//替换<br />
row1.innerHTML=newhtml;<br />
row2.innerHTML=newhtml2;<br />
//方法二<br />
//不明白....下面一句就能实现<br />
//table.appendChild(row1);<br />
}<br />
</script>