jquery
  • events
  • onclick
  • 2012-12-08 104 views 0 likes 
    0

    此脚本不起作用..错误在哪里? With .onload it OKjquery onclick事件不起作用

    $(document).ready(function() { 
    
    var img = new Image(); 
    img.onClick = function() { 
    $("div").animate({width:this.width, height:this.height}); 
    } 
    img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif'; 
    }); 
    

    感谢您的帮助。

    +0

    此无关的Java – kennypu

    +0

    究竟“不工作” ?你可以说它有效吗?至少没有JS错误... – migg

    +0

    @migg http://jsfiddle.net/gphp/CHuBx/5/ – Giovanni

    回答

    4

    .onclick不是.onClick,JavaScript区分大小写。

    +0

    http://jsfiddle.net/gphp/CHuBx/ – Giovanni

    +0

    @Giovanni如果你不添加图像,你怎么知道它不工作?该文件? http://jsfiddle.net/mowglisanu/CHuBx/1/ – Musa

    +0

    我没有在文档中的图像,只有网址。 with onload它的工作http://jsfiddle.net/gphp/CHuBx/3/ – Giovanni

    1

    由于您使用jQuery无论如何,为什么不坚持下去:

    $(function(){ // this is a shortcut for $(document).ready 
        var img = $('<img src="http://www.google.com/intl/en_ALL/images/logo.gif>'); 
        img.on('click', function(){ 
        $('div').animate(...) 
        }) 
        img.appendTo('body') 
    }) 
    
    0

    试试这个:

    var img = new Image(); 
    img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif'; 
    
    $(img).live("click", function() { 
        $("div").animate({ 
         width: $(this).width(), 
         height: $(this).height() 
        }, 1000); 
    }).appendTo('body');​ 
    
    相关问题