有如下代码
<!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">
        function ff(){
            var a=document.createElement("a");
            a.onclick=cc;
            a.appendChild(document.createTextNode("d"));
            document.body.appendChild(a);
            //debugger 
        }
        function cc(u){
            alert(u);
        }
    </script></head>
<body onload="ff()">
    
</body>
</html>怎样才能给cc传递参数"u"?
我不像使用全局变量,因为使用全局变量可能会导致出错。

解决方案 »

  1.   

     <script type="text/javascript">
            function ff(){
                var a=document.createElement("a");
                a.onclick=cc("dsfsd");//这样不就行了吗?
                a.appendChild(document.createTextNode("d"));
                document.body.appendChild(a);
                //debugger 
            }
            function cc(u){
                alert(u);
            }
        </script>
      

  2.   

                a.onclick=cc("dsfsd");//这样不就行了吗? 
      

  3.   

    楼主什么意思?  要不先传个参数个ff(u)在传给cc 不就行了?要想动态传参要写给函数获得你要传的参数返回U在调用ff(u),不就OK了?
      

  4.   

    <script type="text/javascript"> 
            function ff(){ 
                var a=document.createElement("a"); 
                a.onclick=cc("dsfsd");//这样不就行了吗? 
                a.appendChild(document.createTextNode("d")); 
                document.body.appendChild(a); 
                //debugger 
            } 
            function cc(u){ 
                alert(u); 
            } 
        </script>如果是创建<a>标签可以这样
        <script type="text/javascript">
            function ff(u){
                var a=document.createElement("a");
    a.href=u;
             a.appendChild(document.createTextNode("ddd"));
                document.body.appendChild(a);
                //debugger 
            }    </script></head>
    <body onload="ff('http://www.baidu.com')">
      

  5.   

    做个闭包就可以了
    <!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">
            function ff(){
                var a=document.createElement("a");
                (
    function(arg)
    {
    a.onclick=function(){cc(arg);};
    }
        )("aaaa");
                a.appendChild(document.createTextNode("d"));
                document.body.appendChild(a);
                //debugger 
            }
            function cc(u){
                alert(u);
            }
        </script></head>
    <body onload="ff()">
        
    </body>
    </html>
      

  6.   

    var u = "test";
    a.onclick = function(u){ cc(u);};
      

  7.   

    var u = "test";
    a.onclick = function(){ cc(u);}; 
      

  8.   

    7楼的方法确实不错!
    但是我要想动态的创建多个a标签,每个标签的onclick事件都是一样的,就是参数不同。怎么实现?
      

  9.   

    for循环啊  7楼不敢恭维 简直脱了裤子放屁 ,支持2楼
      

  10.   

    <!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">
            function ff(u){
                var a=document.createElement("a");
                a.onclick=function(){
                    cc(u);
                };
                a.appendChild(document.createTextNode(",,,,,,,,,,,,,,,,d"));
                document.body.appendChild(a);
                //debugger 
            }
            function cc(u){
                alert(u);
            }
        </script></head>
    <body onload="ff('55555555')">
        
    </body>
    </html>