我有一组表格,隐藏和显示内容时点击他们的th。此外,我使用scrollTop触及每个点击表的顶部以更好地显示其内容。到目前为止没有问题... 默认情况下,我想让第一个表显示它的内容,但是我想在这种情况下防止页面加载时的scrollTop。我该怎么做?jQuery:防止scrollTop模拟点击()或触发('点击')
$(document).ready(function() {
$('table tr').nextAll().hide();
var $button = $('table th').css('cursor', 'pointer');
$button.click(function() {
$(this).parent().nextUntil('table th').toggle();
$('html, body').animate({
scrollTop: $(this).offset().top
}, 500);
});
$('table th:eq(0)').trigger('click', animate(false));
});
.trigger()上必须有一段代码可以帮助我防止第一次滚动? 感谢您的帮助!
编辑:哦,不是最优雅的解决方案,但分裂事件,并具有设置动画之前触发()似乎工作:
$(document).ready(function() {
$('table tr').nextAll().hide();
var $button = $('table th').css('cursor', 'pointer');
$button.click(function() {
$(this).parent().nextUntil('table th').toggle();
});
$('table th:eq(0)').trigger('click');
$button.click(function() {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 500);
});
});
这个代码的例子是:http://luxurycruisesgalapagos.com/test.html –