很菜的一个问题.望各位大神帮下,function test(obj){
    obj.onclick = function(obj){alert(obj);};
}就是这么简单的一个问题.怎么传参?  这样传是无效的.而如果用function test(obj){
    obj.onclick = function(obj){alert(obj);}(obj);
}这样也是不行的,这样会立即执行.求教

解决方案 »

  1.   

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="gb2312" />
    <title></title> <style>

    </style>
    </head>
    <body>
    <div id="test">123</div>
    <script>
    function test(obj){
        obj.onclick = function(){
    alert(obj.innerHTML);
    };
    }
    test( document.getElementById('test') )
    </script>
    </body>
    </html>
    这样如果这样写的话
    obj.onclick = function(obj){alert(obj);};function(obj) obj 是传递的鼠标事件
      

  2.   

    楼主的写法首先说一下 是不严格的 你的参数都叫同一个名字 很容易把自己绕进去 给你列了2种 你看你要哪一种:
    1、obj都一样
    function test(obj){
        obj.onclick = function(e){
           alert(this);//this就是你的obj这个dom对象
        };
    }2、obj不一样 这种情况是需要闭包来解决的function test(aaa){
    var obj = document.getElementById('元素的id');
        obj.onclick = (function(param){         
            return function(e){
               alert(param);//这哥param就是test函数的形参aaa
               alert(e);//这个就是该dom元素的event事件
             };
        })(aaa)
    }}
      

  3.   

    function test(obj){
        var param="test";
        obj.onclick = function(){
                           clickEvent(param);
                        };
    }
    function clickEvent(param)
    {
      alert(param);
    }
    第二种方式:
    function test(obj){
        obj.pamam="test";
        obj.onclick =clickEvent;
    }
    function clickEvent()
    {
      alert(this.pamam);
    }
      

  4.   

    还有不需要传递参数的方式
    function test(obj){
        var param="test";
        obj.onclick = function(){alert(param);};//局部函数可以直接访问该局部的变量
    }
      

  5.   

    function test(obj){
        obj.onclick = function(){alert(obj);};
    }
    这个不用传参数了