我试图两次运行一个函数。一旦加载页面,然后再次点击。不知道我做错了什么。这里是我的代码:jQuery:调用函数两次
$('div').each(function truncate() {
$(this).addClass('closed').children().slice(0,2).show().find('.truncate').show();
});
$('.truncate').click(function() {
if ($(this).parent().hasClass('closed')) {
$(this).parent().removeClass('closed').addClass('open').children().show();
}
else if ($(this).parent().hasClass('open')) {
$(this).parent().removeClass('open').addClass('closed');
$('div').truncate();
$(this).show();
}
});
的问题是在第13行,我叫truncate();
功能的第二次。任何想法为什么它不工作?
编辑的jsfiddle这里:http://jsfiddle.net/g6PLu/
请将您的html添加到jsFiddle中 – jacktheripper
您在另一个中定义了函数。当你在第13行调用它时,它不能“看到”那个函数。在两个jquery块外定义这个函数,并从每个块中调用它。 –