<table width="357" height="309" border="1">
  <tr>
    <td width="568"><input type="text" name="textfield" id="textfield" wlm_text="text_4" /></td>
    <td width="38" colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td><input type="submit" name="button" id="button" value="提交" wlm_text="text_1"  onclick="ok()"/></td>
    <td width="38" colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td><input type="file" name="fileField" id="fileField"  /></td>
    <td width="38" colspan="2">&nbsp;</td>
  </tr>
</table>for (var i = 0; i< document.all.length;i++)
{e=window.event;
 document.all[i].attachEvent('onmousedown',function(e){

if (e.button==2)
{i++;
alert(e.srcElement.tagName);}
  });}每点击右键单独属性,会弹出好几个。我只想让他弹出一次,这个要怎么实现呢。高手帮帮我啊,累死我了。

解决方案 »

  1.   

    document.all...你只绑定document就好了,document.all就是所有的元素都绑定了,事件是会冒泡的,比如你的代码,td执行了一次,没有中断,向上冒泡到tr又一次,再向上table,document
      

  2.   


    window.onload = function(){
        e=window.event;
        document.body.attachEvent('onmousedown',function(e){             
        if (e.button==2){        
            alert(e.srcElement.tagName);}
        });
    }
      

  3.   

    window.onload=function(){
    for (var i = 0; i< document.all.length;i++)
    {
     
    e=window.event;
                     document.all[i].attachEvent('onmousedown',function(e){
                         
                        if (e.button==2)
                        {i++;
                        alert(e.srcElement.tagName);
    e.cancelBubble=true;
    }
                      });
     
    }
    }