【发布时间】:2016-07-20 09:26:02
【问题描述】:
我正在尝试制作一个微不足道的“scrollTo 元素”功能。
控制台向我显示错误: 语法错误,无法识别的表达式:a[href^=#] for a[href^=#] 代码
根据this question 上的回复,我将井号用双引号括起来,但现在控制台为此显示 Unexpected token ILLEGAL。
请解释我做错了什么以及如何解决它。
这是我的代码:
$(document).on('click', 'a[href^=#]', function () {
$('html, body').animate({ scrollTop: $('section[data-target="'+this.hash.slice(1)+'"]').offset().top }, 1000 );
return false;
});
menu {
background-color: #1abc9c;
height: 50px;
position: fixed;
left: 0;
top: 0;
width: 100%;
margin: 0;
padding: 0;
}
menu ul li {
display: inline-block;
padding: 0 15px;
}
menu ul li a {
color: #333;
text-decoration: none;
}
section {
height: 300px;
padding: 60px 0 0 45px;
}
.one {
background-color: #3498db;
}
.two {
background-color: #e74c3c;
}
.three {
background-color: #f39c12;
}
.four {
background-color: #2c3e50;
}
<menu>
<ul>
<li><a href="#one">One</a></li>
<li><a href="#two">Two</a></li>
<li><a href="#three">Three</a></li>
<li><a href="#four">Four</a></li>
</ul>
</menu>
<section class="one" data-target="one">Section One</section>
<section class="two" data-target="two">Section Two</section>
<section class="three" data-target="three">Section Three</section>
<section class="four" data-target="four">Section Four</section>
【问题讨论】:
-
你到底是怎么包装的?包装它对我来说很好。
标签: jquery html jquery-selectors