【发布时间】:2018-10-13 13:57:20
【问题描述】:
实际上,我通过单击按钮在数据库中的表中显示数据。我有多个按钮和多个表格。我想根据按钮单击将滚动条移动到特定的 div。每次当我单击任何按钮时,滚动条都会移到顶部。 这是我用来在特定位置移动滚动条的代码。
<input type="submit" name="search" id ="search" onclick="myFunctionq();" value="Display" />
<script>
function myFunctionq(){
var divLoc = $('#div1').offset();
$('html, body').animate({scrollTop: divLoc.top},"slow");
};
</script>
这对我不起作用。
如果我在 $(document).ready(function()) 中编写相同的代码,它正在工作并且每次滚动条都在移动。
<script type="text/javascript">
$(document).ready(function(){
var divLoc = $('#div1').offset();
$('html, body').animate({scrollTop: divLoc.top}, "slow");
});
</script>
我想根据按钮点击事件将滚动位置移动到 div。
【问题讨论】:
-
添加return false取消默认事件
标签: javascript jquery