img.onclick = new Function("alert('"+ picUrl +"')");

解决方案 »

  1.   

    试试attatchEvent:img.attatchEvent("onclick",fClick);
    fucntion fClick()
    {
       var e = event.srcElement;
       alert(e.url);
    }
      

  2.   

    fucntion fClick()
    {
       var e = event.srcElement;
       alert(e.src); //src
    }
      

  3.   

    setAttribute添加事件应该这样
    <input type=button value=click id=btn>
    <script>
    btn.setAttribute("onclick",function(){alert(11)})
    </script>
      

  4.   

    谢谢各位
    meizz(梅花雪) 的方法能行
    但还是没明白为什么要new function
    CutBug(Zergling in Net)的方法我见过,但在我的电脑上在attatchEvent出错
    Line:216
    Char:7
    Code:0
    Error:对象不支持此属性或方法另我知道了btn.setAttribute("onclick",function(){alert(11)})谢谢你
      

  5.   

    img.onclick=function func1(picUrl){alert(picUrl);}
    你这样写,你的 picUrl 是一个局部变量,且在循环的时候不断地被改变,所以只会得到最后的一个值,而用 new Function 的方法就是把变量“固化”到函数里。
      

  6.   

    img.onclick=function func1(picUrl){alert(picUrl);}
    我原来理解成,这个function func1加参数会写到代码中,也就是固化,所以每次循环不一样的参数可不可以这样理解,这个func1其实只有一个,是在内存在,所以循环完了,只有一个func1及最后一次循环的参数,所以每个onclick都指向了这同一个而new function 就为每个on click 生成了相应的 一个新的function 及参数在内存中,所以能
    ...
      

  7.   


    <input type=button id=b1 value=b1>
    <input type=button id=b2 value=b2>
    <input type=button id=b3 value=b3>
    <input type=button id=b4 value=b4>
    <script type="text/javascript">
    for(var i=1;i<5;i++)
    {
    var s = "a"+i;
    document.getElementById("b"+i).onclick =  function(){alert(s)};
    }
    </script>function只是生成一个临时无名函数,等价于一个过程,在执行过程中会被不断的赋值,循环执行完,s="a5" ;
    而Function则会生成一个新对象函数,是放到内存中的
      

  8.   

    给分出错,又是javascript问题?
    Line:16
    Char:5
    Code:0
    Error:没有注册类别URL:http://community.csdn.net/expert/Topicview2.asp?id=5242695出错在这里<script language="javascript">
    // 感谢csdn网友 梅花雨 提供这段代码
    /*@cc_on @*/
    /*@if (@_win32 && @_jscript_version>5)
    function window.confirm(str)
    {
        execScript("n = msgbox('"+ str +"', 257)", "vbscript");
        return(n == 1);//这是16行}
    @end @*/
      

  9.   

    学习了很多,谢谢meizz(梅花雪),CutBug(Zergling in Net) 
      

  10.   

    img.onclick = new Function("alert('"+ picUrl +"')");
      

  11.   

    img.onclick=function(){
     alert(this.src);
    }