jquery是这么写。这是排除了第一列onclick。
$("TR td:gt(0)").click
我要排除首尾两列的td触发事件。
请问用原生态js怎么写。谢谢各位。

解决方案 »

  1.   

    <table id="tb">
    <tr>
    <td>1111111</td>
    <td>1111111</td>
    <td>1111111</td>
    <td>1111111</td>
    <td>1111111</td>
    <td>1111111</td>
    </tr>
    </table>
    </body>
       <script type="text/javascript">
    var td = document.getElementById("tb").getElementsByTagName("tr")[0].getElementsByTagName("td")
    for(var i = 0 ; i < td.length ; i ++)
    {
        td[i].onclick = function(){alert("x")}
    }
    for(var i = 0 ; i < td.length ; i ++)
    {
        if(i == 0 || i ==td.length - 1) td[i].onclick = null
    }
        </script>
      

  2.   

    楼上那位大爷。。怎么这么写啊。。你要我改死啊目前是这样。SItem[i].Sbtr 这个就是一条记录TR。
    SItem[i].Sbtr.onmouseover = function ()
    {
    this.className = (this.className != '') ?  this.className + ' onmouseover' : 'onmouseover';
    }
    SItem[i].Sbtr.onmouseout = function ()
    {
    this.className = this.className.replace(/onmouseover/, '');
    }SItem[i].Sbtr.onmousedown = function (event)
    { // 按住Shift键
    event = event? event:window.event;
    if((!document.all && event.button == 1) ||event.button == 4)  return; if(event.shiftKey==true && StartTr != null)
    {
    EndTr = this.key;
    var StartGo = StartTr; // 开始的Tr KEY
    var Sum = StartTr - EndTr; // 个数
    if(Sum > 0) var StartGo = EndTr;
    Sum = Math.abs(Sum);
    if(Sum < 1) return;
    var EndGo = Sum + StartGo +1;
    for (; StartGo < EndGo; ++StartGo)
    {
    ClickTr(SItem[StartGo].Sbtr, false);
    }
    SEstatus = false;
    return;
    }
    else
    {
    StartTr = this.key;
    SEstatus = true;
    if(event.button == 2)  // 右键选择
    ClickTr(this, false); // 只能选上
    else
    ClickTr(this, true); // 自由切换
    }

    }
      

  3.   

    for(var i = 1 ; i < td.length-1 ; i ++){
        //循环绑定的时候 处理下首尾 不就行了 。
    }
      

  4.   

    选中元素后,移除事件不行么?
    td.removeEventListener(“click”, handler, false);
      

  5.   

    var td = document.getElementsByTagName("td");
    for(var i=0; i<td.length; i++) {
      if(i!=0 && i!=td.length-1) {//排除首尾
       td[i].onclick = function() {
          //......
       }
      }
    }
      

  6.   

    阻止它冒泡吧,列子:<!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>
      
    </head>
    <body><table border="1">
        <tr onclick="fn()">
            <td id="t1">取消了事件</td>
            <td >点我,我没取消</td>
            <td id="t2">取消了事件</td>
        </tr>
    </table>
      <script type="text/javascript">      function stopBubble(e) {
              if (e && e.stopPropagation) {
                  e.stopPropagation();
              }
              else {
                  window.event.cancelBubble = true;
              }
          }
        
          document.getElementById('t1').onclick = function (e) {
              stopBubble(e);
          }
          document.getElementById('t2').onclick = function (e) {
              stopBubble(e);
          }
          function fn() {
              alert('s');
          }    </script>
    </body>
    </html>