function x(){
 alert(this)
}<input id="x" type="text" />document.getElementById("x").onclick=x如果用这个写法 在函数x中 可以用 this引用 这个input但是如果 onclick="x()" 这样写 那如何在 方法中引用 引用这个 input用 this的话 引用的是 window对象该如何做?

解决方案 »

  1.   

    <!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> 
      <script type="text/javascript"> 
    function x(){
    alert(this)
    }
    window.onload = function() {
    document.getElementById("x").onclick=x
    };
      </script> 
      </head> 
      
      <body> 
          <input id="x" type="text" value="hit me" />
      </body> 
      </html>
      

  2.   

    function x(){
        alert(document.activeElement);
    }
      

  3.   

    <input  value='aaa' onclick='ss(this)'/>function ss(obj){
    alert(obj.value);
    }
      

  4.   

    楼上正解把this传进去楼主结帖率:35.29%  有点恐怖呀