单击锚链接时,我试图滚动到div,但是由于某些原因,当使用jQuery选择器时,它给了我错误Syntax error, unrecognized expression: ../#Installation
这是jQuery函数:
$('a').click(function(ev){
ev.preventDefault();
$('html, body').animate({
scrollTop: $( $(this).attr('href') ).offset().top
}, 500);
});
定位标记:
<a href="#Installation" id="install" runat="server">Installation</a>
div:
...
<div id="Installation"></div>
...
如果我专门写了div id,那么它就可以正常工作:
$("#install").click(function(ev) {
ev.preventDefault();
$('html, body').animate({ scrollTop:$("#Installation").offset().top}, 500);
});
我在这里做错了什么?
答案 0 :(得分:0)
动画前的用户“ var”:
<div id="Installation">I am Installation</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#Installation" id="install" runat="server">Go to Installation</a>
<script>
$('a').click(function(ev){
ev.preventDefault();
var href=$(this).attr('href');
$('html, body').animate({
scrollTop: $(href).offset().top
},500);
});
</script>