Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 LINQ ADO.NET JavaScript DataGridView GridView AjaxPro DevExpress 面向对象 Extjs XML HTML教程 Oracle jQuery WPF MVC 分页 Office2010 GDI+ Visual C++2010 Dojo MySQL VB.NET WCF4.0 ASPxGridView WinForm textbox Sql2005 cookie Discuz!NT checkbox WCF SQL经典语句 T-SQL F# asp.net VS2008 SQL Ajax DropDownList VS2008新特性 TreeView Access Flex 页面执行时间 字符串 DataSet VB2005 回调 动画 C#时间 游戏 ASP.NET MVC
新闻动态面试题及技巧

javascript操作table增加,批量删除,模糊查询,文本框修改(兼容IE,FF)

时间:2011-08-12 12:57来源:未知 作者:admin 点击:

在网上没有找到完全的例子,而且不是我想要的,自己Javascript水平有限,一直做C#-Winform通信,但是对Javascript感兴趣,凭借大学那点皮毛掏空了脑袋按照自己的思路写了一遍,当然这里的代码全部自己写的,没参考网上。。所以比较烂,没有借鉴到其他代码的优点,以后会继续完善。希望各位看到的不吝提出意见:改进办法,代码优化,兼容性。。。

先看代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script type="text/javascript">
        var _OTable_ = null;
        var _oTbody_ = null;
        var _arrSelect_ = new Array;
    
        var _oTempValue_=new Object;
        _oTempValue_["$updateIndex"]=-1;
        var _TheadName_=new Array("姓名","性别","年龄","籍贯","删除否");
        var CELLS_COUNT=_TheadName_.length-1;
       
        String.prototype.trim=function()
        {
             return this.replace(/(^\s*)(\s*$)/g, '');
        }

        window.onload = function()
        {
            var $oAdd = document.getElementById("btnAdd");
            $oAdd.onclick = function()
            {
                var _oCol1_ = document.getElementById("Col1");
                var _oCol2_ = document.getElementById("Col2");
                var _oCol3_ = document.getElementById("Col3");
                var _oCol4_ = document.getElementById("Col4");

                if (!_OTable_) //如果不存在表则新建
                {
                    _OTable_ = document.createElement("table");
                    _OTable_.setAttribute("border", "1");
                    _OTable_.setAttribute("width", "800px");
                    var _Thead_=_OTable_.createTHead();
                    var _TRow_=_Thead_.insertRow(0);
                    for(var _headindex_=0;_headindex_<CELLS_COUNT+1;_headindex_++ )
                    {
                       var _tTh=_TRow_.insertCell(_headindex_);
                       _tTh.appendChild(document.createTextNode(_TheadName_[_headindex_]));
                    }
                    _oTbody_ = document.createElement("tbody");
                    _OTable_.appendChild(_oTbody_);
                    document.getElementById("TableData").appendChild(_OTable_);
                }
                if(!_oCol1_.value.trim()){alert("姓名必须填写!"); return;}
               
                //添加一行
                var _oRow_ = _oTbody_.insertRow(-1);

                //添加5列,四列值,一列选择
                var _oCell1_ = _oRow_.insertCell(-1);
                _oCell1_.appendChild(document.createTextNode(_oCol1_.value));
                var _oCell2_ = _oRow_.insertCell(-1);
                _oCell2_.appendChild(document.createTextNode(_oCol2_.value));
                var _oCell3_ = _oRow_.insertCell(-1);
                _oCell3_.appendChild(document.createTextNode(_oCol3_.value));
                var _oCell4_ = _oRow_.insertCell(-1);
                _oCell4_.appendChild(document.createTextNode(_oCol4_.value));

                _oCol1_.value = "";
                _oCol2_.value = "";
                _oCol3_.value = "";
                _oCol4_.value = "";

                //选择
                var _oCell5_ = _oRow_.insertCell(4);
                _oCell5_.setAttribute("style", "width:50px;");
                var _oCheckBox_ = document.createElement("input");
                _oCheckBox_.setAttribute("type", "checkbox");
                _oCell5_.appendChild(_oCheckBox_);
                _oCheckBox_.onclick = function()
                {
                    if (_oCheckBox_.checked)
                    {
                        var _rowIndex_ = _oCheckBox_.parentNode.parentNode.rowIndex-1;
                        _arrSelect_.push(_rowIndex_);
                    }

                }

                _oRow_.ondblclick = function()
                {
                    var _oPreUpdateIndex_=_oTempValue_["$updateIndex"];
                    var _oPreTempRow_=null;
                    if (parseInt(_oPreUpdateIndex_) != -1) //原先选定行重置
                    {
                     
                      if (!_OTable_ || !_oTbody_) return;
                      _oPreTempRow_= _oTbody_.rows[parseInt(_oPreUpdateIndex_)];
                      var _cancelornot_=false;
                      for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                      {
                         var $attributeNode_=_oPreTempRow_.cells[_cellindex_].firstChild;
                         var $nodedata_=document.all?$attributeNode_.getAttribute("value"):$attributeNode_.value;
                         if($nodedata_!=_oTempValue_["$"+_cellindex_])//与原值比较
                         {
                            _cancelornot_=confirm("你之前的内容作了修改,保存修改吗?");
                            break;
                         }
                      }
                      if(_cancelornot_)
                      {
                          //避免前次提交为空
                          var _firstNode_=_oPreTempRow_.cells[0].firstChild;
                          var $firstnodedata_=_firstNode_.getAttribute("value");
                          if(!$firstnodedata_||!$firstnodedata_.trim()){alert("姓名不能为空,请重新编辑!"); _firstNode_.focus(); return;};
                         
                          for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                          {
                              var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;
                              var $nodedata_=document.all?_oldNode_.getAttribute("value"):_oldNode_.value;
                              var _textnode_=  document.createTextNode($nodedata_);
                              _oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);
                              delete _oTempValue_["$"+_cellindex_];
                          }
                      }
                      else
                      {
                         for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                          {
                              var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;
                              var _textnode_=  document.createTextNode(_oTempValue_["$"+_cellindex_]);
                              _oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);
                              delete _oTempValue_["$"+_cellindex_];
                          }
                      }

                    }
                    _oTempValue_["$updateIndex"] = this.rowIndex-1;
                    //单元格中只有一个文本节点
                    for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                    {
                      var _textbox_=  document.createElement("input");
                      _textbox_.setAttribute("type", "text");
                      var _preNode_=this.cells[_cellindex_].firstChild;
                      _oTempValue_["$"+_cellindex_]=_preNode_.nodeValue; //记录原先的值
                      _textbox_.setAttribute("value",_preNode_.nodeValue);
                   
                      this.cells[_cellindex_].replaceChild(_textbox_ ,_preNode_);
                    }
                };
            };

            //删除
            var $oDelete = document.getElementById("btnDelete");
            $oDelete.onclick = function()
            {
                if (_arrSelect_.length == 0) { alert("您还没有选择要删除的行."); return; }

                if (_OTable_  && _oTbody_)
                {
                    var _confirmMsg_ = "你确定要删除名字是如下:\n";
                    for (var index = 0, iLen = _arrSelect_.length; index < iLen; index++)
                    {
                        var _deletName_ = _oTbody_.rows[parseInt(_arrSelect_[index])].cells[0].firstChild.nodeValue;
                        _confirmMsg_ = _confirmMsg_.concat(_deletName_ + "\n");
                    }
                    _confirmMsg_ = _confirmMsg_.concat("的信息吗?");
                    if (!confirm(_confirmMsg_)) return;

                    for (var index = _arrSelect_.length - 1; index >= 0; index--)
                    {
                        _oTbody_.deleteRow(parseInt(_arrSelect_[index]));
                    }
                }
                _arrSelect_.splice(0,_arrSelect_.length); //清空选择列表
            };

        //更新
            var $oUpdate = document.getElementById("btnUpdate");
            $oUpdate.onclick = function()
            {
                var _oPreUpdateIndex_=_oTempValue_["$updateIndex"]
                if (parseInt(_oPreUpdateIndex_)== -1){alert("您未编辑任何更新行!") ;return;}
                if (_OTable_ && _oTbody_ )
                {
                   if(confirm("您确定修改吗?"))
                   {
                       var _temprow_= _oTbody_.rows[parseInt(_oPreUpdateIndex_)];
                       var $namenode=_temprow_.cells[0].firstChild;
                       var $namenodevalue=document.all?$namenode.getAttribute("value"):$namenode.value;
                       if(!$namenodevalue||!$namenodevalue.trim()){ alert("姓名不能为空,请重新编辑!"); $namenode.focus(); return;}
                       for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                       {
                          var $tmpnode_=_temprow_.cells[_cellindex_].firstChild;
                          var $nodedata_=document.all?$tmpnode_.getAttribute("value"):$tmpnode_.value;
                          var _textnode_=  document.createTextNode($nodedata_);
                          var _oldNode_=_temprow_.cells[_cellindex_].firstChild;
                          _temprow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);
                          delete _oTempValue_["$"+_cellindex_];
                       }
                   }
                }
                _oTempValue_["$updateIndex"] = -1
            };
           
               //查找
            var $oFind = document.getElementById("btnFind");
             $oFind.onclick=function()
             {
               if(!_OTable_ && !_oTbody_ ){alert("目前尚无数据可查!");return;}
              
               ///////////////判断之前有编辑未提交的
               var _oPreUpdateIndex_=_oTempValue_["$updateIndex"];
               var _oPreTempRow_=null;
                if (parseInt(_oPreUpdateIndex_) != -1) //原先选定行重置
                {
                 
                  if (!_OTable_ || !_oTbody_) return;
                  _oPreTempRow_= _oTbody_.rows[parseInt(_oPreUpdateIndex_)];
                  var _cancelornot_=false;
                  for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                  {
                     var $childNode_=_oPreTempRow_.cells[_cellindex_].firstChild;
                     var $nodedata_=document.all?$childNode_.getAttribute("value"):$childNode_.value;
                     if($nodedata_!=_oTempValue_["$"+_cellindex_])//与原值比较
                     {
                        _cancelornot_=confirm("你之前的内容作了修改,保存修改吗?");
                        break;
                     }
                  }
                  if(_cancelornot_)
                  {
                      //避免前次提交为空
                      var _firstNode_=_oPreTempRow_.cells[0].firstChild;
                      var $firstnodedata_=document.all?_firstNode_.getAttribute("value"):_firstNode_.value;
                      if(!$firstnodedata_||!$firstnodedata_.trim()){alert("姓名不能为空,请重新编辑!"); _firstNode_.focus(); return;};
                     
                      for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                      {
                          var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;
                          var $nodedata_=document.all?_oldNode_.getAttribute("value"):_oldNode_.value;
                          var _textnode_=  document.createTextNode($nodedata_);
                          _oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);
                          delete _oTempValue_["$"+_cellindex_];
                      }
                  }
                  else
                  {
                     for(var _cellindex_=0;_cellindex_<CELLS_COUNT;_cellindex_++)
                      {
                          var _oldNode_=_oPreTempRow_.cells[_cellindex_].firstChild;
                          var _textnode_=  document.createTextNode(_oTempValue_["$"+_cellindex_]);
                          _oPreTempRow_.cells[_cellindex_].replaceChild(_textnode_,_oldNode_);
                          delete _oTempValue_["$"+_cellindex_];
                      }
                  }
                }
               
                //清除更新列表
                for(var $obj_ in _oTempValue_)
                {
                   delete _oTempValue_[$obj_];
                }
               // _oTempValue_=new Object;
                _oTempValue_["$updateIndex"] = -1;
              
               ////////////////////////开始查询
               var _$oSelect_= document.getElementById("selectCol");
               var _Index_=_$oSelect_.selectedIndex;
               var _$oSelectValue_= _$oSelect_.value;
               var _$oSelectText_= _$oSelect_.options[_Index_].text;
               var _$olike_=document.getElementById("Col9");
               var _$rowcollection_=_oTbody_.rows;
               var _$rLen=_$rowcollection_.length;
               switch(parseInt(_$oSelectValue_))
               {
                 case 0:
                      for(var _rIndex=0;_rIndex<_$rLen;_rIndex++)
                      {
                         var _selectrow_=_$rowcollection_[_rIndex];
                         var $pat = new RegExp(_$olike_.value.trim(),"i");
                         if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}//如果查询框为空,则全部提取..模糊搜索
                         else {if(!$pat.test(_selectrow_.cells[0].firstChild.nodeValue.trim())){
                            _selectrow_.style.display="none";}}
                      }
                  break;
                  case 1:
                      for(var _rIndex=0;_rIndex<_$rLen;_rIndex++)
                      {
                         var _selectrow_=_$rowcollection_[_rIndex];
                         var $pat = new RegExp(_$olike_.value.trim(),"i");
                         if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}
                         else
                         {if(!$pat.test(_selectrow_.cells[1].firstChild.nodeValue.trim()))
                         {_selectrow_.style.display="none";}}
                      }
                  break;
                  case 2:
                      for(var _rIndex=0;_rIndex<_$rLen;_rIndex++)
                      {
                         var _selectrow_=_$rowcollection_[_rIndex];
                         var $pat = new RegExp(_$olike_.value.trim(),"i");
                         if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}
                         else
                         {if(!$pat.test(_selectrow_.cells[2].firstChild.nodeValue.trim()))
                         { _selectrow_.style.display="none";}}
                      }
                  break;
                  case 3:
                      for(var _rIndex=0;_rIndex<_$rLen;_rIndex++)
                      {
                         var _selectrow_=_$rowcollection_[_rIndex];
                         var $pat = new RegExp(_$olike_.value.trim(),"i");
                         if(!_$olike_.value.trim()){_selectrow_.style.display=document.all?"block":"table-row";}
                        else
                         {if(!$pat.test(_selectrow_.cells[3].firstChild.nodeValue.trim()))
                         { _selectrow_.style.display="none";}}
                      }
                  break;
               }
            
                _arrSelect_.splice(0,_arrSelect_.length);//清除删除列表
        
    </script>

                var _checkBoxList_=document.getElementsByTagName("input"); //重置checkbox选择.
                for(var _index=0,iLen=_checkBoxList_.length;_index<iLen;_index++)
                {
                  if(_checkBoxList_[_index].type=="checkbox")
                  {
                      _checkBoxList_[_index].checked=false;
                  }
                }
             };
            
              var $oSelectAll = document.getElementById("btnSelectAll");
              $oSelectAll.onclick=function()
              {
                 if(_OTable_ && _oTbody_ )
                 {
                      var _$rowall_=_oTbody_.rows;
                      for(var _rIndex=0,_rLen=_$rowall_.length;_rIndex<_rLen;_rIndex++)
                      {
                         var _selectrow_=_$rowall_[_rIndex];
                         _selectrow_.style.display=document.all?"block":"table-row";
                      }
                 }
              }
        }

</head>
<body>
    <fieldset>
        <legend>操作Table之增删查改</legend>
        <fieldset>
            <legend>添加</legend>
            <label for="Col1">
                姓名:
            </label>
            <input type="text" id="Col1" />
            <label for="Col2">
                性别:
            </label>
            <input type="text" id="Col2" />
            <label for="Col3">
                年龄:
            </label>
            <input type="text" id="Col3" />
            <label for="Col4">
                籍贯:
            </label>
            <input type="text" id="Col4" />
            <input type="button" value="添加" id="btnAdd" />
        </fieldset>
        <fieldset>
            <legend>查找</legend>
            <label for="Col9">
                查找内容:
            </label>

            <script type="text/javascript">
            var options=["<option value=\"0\" selected>姓名</option>","<option value=\"1\">性别</option>","<option value=\"2\">年龄</option>","<option value=\"3\">籍贯</option>"];
            document.write("<select name=\"selectCol\" id=\"selectCol\">"+options.join("")+"</select>");
            </script>

            <input type="text" id="Col9" />
            <input type="button" value="查找" id="btnFind" />
        </fieldset>
    </fieldset>
    <br />
    <fieldset id="TableData">
        <legend>表格数据</legend>
    </fieldset>
    <input type="button" value="删除" id="btnDelete" />
    <input type="button" value="更新" id="btnUpdate" />
    <input type="button" value="显示全部" id="btnSelectAll" />
</body>
</html>

Tags:JavaScript
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价
  • Asp.net面试题(二)

    01.{DotNet题目}下列变量的赋值,正确的是:( )(选择2项) A )int myInteger=17; B ) string myString = \...

  • 谈谈.Net技术面试

    谈谈.Net技术面试...

  • 一道关于实例化顺序的C#面试题

    一道关于实例化顺序的C#面试题...

  • 上海某软件公司.Net笔试题

    上海某软件公司.Net笔试题...

  • 数据结构若干经典问题和算法

    一、迭代法 迭代法是用于求方程或方程组近似根的一种常用的算法设计方法。设方程为f(x)=0,用某种数学方法导出等价的形式x=g(x),然后按以下步骤执行: (1...

  • 如何写一份好的工程师简历

    最近三年作为 Google(谷歌)的软件工程师,我每周会帮人事部门审查简历,决定要不要给他们面试。Google 这几年的发展让很多许多优秀的工程师都前来申请。到...

  • 面试技巧:16个经典面试问题回答思路

    问题一:请你自我介绍一下 思路: 1、这是面试的必考题目。 2、介绍内容要与个人简历相一致。 3、表述方式上尽量口语化。 4、要切中要害,不谈无关、无用的内容。...

  • 继"一题比较刁的面试题" 后,sliverlight实现

    2009年11月19日来到苏州热线面试(苏州比较大的门户网站) 面试结束后带回去了一个题、 要求: 1.任何语言 任何形式(web,winform,flash,...

  • 一题比较刁的面试题

    昨天晚上看到群里在聊面试题,回想最近几个月的面试经历,很是感慨。 10月底从昆山回到苏州。直到11月底才找到工作,跑到哪都是有没有工作经验,没有。。。好吧,回去...

  • 一份雅虎公司C#笔试题

    Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是 1. 传输率 2. 误码率 3. 信息容量 4. 频带利用率 Question 2...

  • C#常见问题及中等程序员面试常见题

    1 Thread和Process之间有什么区别?.NET新引入了Application Domain的概念,这样他们三个之间有什么区别?引入了Applicati...

  • .NET面试题整理-数据库篇

    1、索引 好处: (1)提高查询表内数据的速度。(2)表与表之间的连接速度加快。 坏处: (1)创建索引和维护索引都会消耗时间。(2)会占用一定物理空间,占用物...

  • .NET面试题总结一

    1:关于ASP.NET中的代码隐藏文件的描述正确的是: C A.Web窗体页的程序的逻辑由代码组成,这些代码的创建用于与窗体交互。编程逻辑唯一与用户界面不同的文...

  • 2010年.NET面试题整理之基础篇

    1、什么是面向对象 面向对象OO = 面向对象的分析OOA + 面向对象的设计OOD + 面向对象的编程OOP; 通俗的解释就是万物皆对象,把所有的事物都看作一...

  • 某外企SQL Server面試題

    Question 1:Can you use a batch SQL or store procedure to calculating the Number ...

  • 大型软件公司.net面试题!一定得看(附答案)

    1:a=10,b=15,在不用第三方变量的前提下,把a,b的值互换 2:已知数组int[] max={6,5,2,9,7,4,0};用快速排序算法按降序对其进行...

  • 雅虎公司C#笔试题

    雅虎公司C#笔试题,包括问答题和选择题两部分。试试看,你能回答出多少题,可以通过雅虎的考试吗? Question 1. (单选) 在计算机网络中,表征数据传输可...

  • 七道最经典的asp.net页面传值题

    第一题:一个Page:PageA. asp x的 网页 上放有一个TextBox ID为TextBox1和一个Button ID为Button1; 一个User...

  • DotNet中级面试题目大全

    1、用C#或者JAVA写一个MAIN函数 2、面向对象的几种方式,你在实际应用中用到了哪些,请描述 3、写出asp.net或者jsp写出动态生成网页的过程,并说...

  • DotNet初级面试题目大全

    1、你平时会看哪些方面的书籍?喜欢浏览哪些网站? 2、当你遇到问题时通常如何解决? 3、谈谈你对.Net与Java的看法 4、你对公司要求加班有什么样的看法? ...

  • 程序员五大层次

    程序员是软件的代名词!在 软件界一个无可争议的事实是,不同程序员的效率有差别,而且差别很大。许多专家将优秀程序员和一般程序员区分地很清楚。 大多数研究得出结论认...

  • 一个程序员的职业规划

    大多数的程序员,对自己的职业发展方向都是模糊的。不做程序员了还能做什么,很多人都不知道。下面,笔者与大家分享几个观点。 选择比能力重要 通常,作为程序员,一般会...