简体   繁体   English

Onclick无法正常运作

[英]Onclick is not working

Why will this code not work as an onclick ? 为什么此代码不能作为onclick使用?

$('.mainz11').click (function() {
    $(this).animate({
        height: '280px'
    }, 800);
}, function() {
    $(this).animate({
        height: '100px'
    }, 800);
});

If you're trying to first expand the element and then contract it, it should probably be something like this: 如果您尝试先扩展元素然后收缩它,则可能应该是这样的:

$('.mainz11').click(function() {
    // determine target heights
    if ($(this).hasClass("expanded")) {
            var targetHeight = 100;
    } else {
        var targetHeight = 280;
    }

    // animate
    $(this).animate({
        height: targetHeight
    }, { 
        duration: 800,
        complete: function() { $(this).toggleClass("expanded"); }
    });
});

This could use some cleaning up, but it does the trick, and you can track expanded items easily this way. 这可以使用一些清理方法,但是可以解决问题,并且您可以通过这种方式轻松跟踪扩展的项目。 See here: http://jsfiddle.net/mpQek/3/ 看到这里: http : //jsfiddle.net/mpQek/3/

The click function takes only a single function but you are passing 2 functions to it. click函数仅使用一个函数,但是您要向其传递2个函数。 You can try it this way: 您可以这样尝试:

 $('.mainz11').click (function() {
    $(this).animate({
    height: '280px'
    }, 800);
   });

If you want to chain animations, put the next animation as the function to run on complete of the first animation: 如果要链接动画,请将下一个动画作为要在第一个动画的完整部分上运行的函数:

http://api.jquery.com/animate/ http://api.jquery.com/animate/

    $('.mainz11').click (function() { 
  $(this).animate({ height: '280px', 800, 
    function() { $('.mainz11').animate({ height: '100px'}, 800)
  ); 
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM