0

的代码$('.button-up').click(function(){ // }没有被触发。我找不到问题。这是我正在尝试做的在线演示。

http://jsfiddle.net/UfnCn/

$('.button-up').click(function(){
    $('.description').animate({height: '80px'}, 400);
    $(this).removeClass('button-up');
    $(this).html('read more');
    $(this).addClass('button-down');
    return false;
});
$('.button-down').click(function(){
    var height = $('.description').css('height','auto').height() + 20; 
    $('.description').css('height','125px'); 
    $('.description').animate({height: height+'px'}, 400);
    $(this).addClass('button-up');
    $(this).html('collapse');
    $(this).removeClass('button-down');
    return false;
});
4

2 回答 2

2

您应该使用事件委托和.on. 事件处理程序在声明时被绑定,因此由于在button-up附加处理程序时未将类分配给元素,因此没有附加任何内容。

你应该能够做类似的事情$("p.overlay").on("click", ".button-up", function(){..});。将处理程序附加到按钮祖先中不会更改的任何元素(文档始终是安全的选择),然后使用选择器进行过滤。

于 2013-06-30T14:29:58.113 回答
1

那是因为当您将 click 事件与 jqueryclick函数绑定时,没有元素具有button-up类。因此,一种方法是使用 jqueryon方法或使用toggle方法将一种状态连续切换到另一种状态。

于 2013-06-30T14:35:15.093 回答