分享

(5)JavaScript获取表格单元格内容

 Tehero 2015-05-31

JavaScript获取表格单元格内容

Js代码 

var ccell document.getElementByIdx_x("tableid").rows[0].cells[0];     

document.getElementByIdx_x("txtname").value ccell.innerText;  

  

//获的总行数     

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; 

    

   ); 

}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多