JavaScript获取表格单元格内容
Js代码
1 var ccell = document.getElementByIdx_x("tableid").rows[0].cells[0];
2 document.getElementByIdx_x("txtname").value = ccell.innerText;
3
4 //获的总行数
5 document.getElementByIdx_x("tableid").rows.length
IE和火狐下获取单元格内容的共通方法
Firefox中的:
<p>
<script language="javascript">
var str=aaa.rows[2].cells[2].textContent;
document.write(str);
</script>
</p>
IE中的:
<p>
<script language="javascript">
var str=aaa.rows[2].cells[2].innerText;
document.write(str);
</script>
</p>
IE和火狐下获取单元格内容所使用的方法是不一样的,IE用的innerText,而火狐是用textContent,可以通过下面所列的方法实现两个浏览器的通用.在使用时只需要把这两个函数放到JS文件的开始位置或其他JS方法之前就行。方法如下:
function isIE(){ //ie?
if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)
return true;
else
return false;
}
if(!isIE()){ //firefox innerText define
HTMLElement.prototype.__defineGetter__( "innerText",
function(){
var anyString = "";
var childS = this.childNodes;
for(var i=0; i<childS.length; i++) {
if(childS[i].nodeType==1)
anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText;
else if(childS[i].nodeType==3)
anyString += childS[i].nodeValue;
}
return anyString;
}
);
HTMLElement.prototype.__defineSetter__( "innerText",
function(sText){
this.textContent=sText;
}
);
}
|