Jquery 未执行脚本
我对网络语言几乎没有经验,所以这可能是一个简单的错误 我有一个内部链接的网页,如中所示。然而,由于具有响应性的引导菜单,滚动有时会超调。问题与前一个问题中描述的完全相同,我尝试实施公认的解决方案: 但是,建议的函数永远不会执行。我已将其实现如下。作为一种简单的测试方法,我包含了两个警报语句,一个用于无关函数。第一条alert语句被正确执行,第二条永远不会执行Jquery 未执行脚本,jquery,css,twitter-bootstrap,Jquery,Css,Twitter Bootstrap,我对网络语言几乎没有经验,所以这可能是一个简单的错误 我有一个内部链接的网页,如中所示。然而,由于具有响应性的引导菜单,滚动有时会超调。问题与前一个问题中描述的完全相同,我尝试实施公认的解决方案: 但是,建议的函数永远不会执行。我已将其实现如下。作为一种简单的测试方法,我包含了两个警报语句,一个用于无关函数。第一条alert语句被正确执行,第二条永远不会执行 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jq
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(function () { // on document ready
var div = $('#showOrHideDiv'); // cache <div>
$('#action').click(function () { // on click on the `<a>`
alert('here'); // => PROPERLY EXECUTED
div.fadeToggle(1000); // toggle div visibility over 1 second
});
// listen for click events originating from elements with href starting with #
$('body').on('click.scroll-adjust', '[href^="#"]', function (e) {
alert('here'); // => NEVER EXECUTED
var $nav;
// make sure navigation hasn't already been prevented
if ( e && e.isDefaultPrevented() ) return
// get a reference to the offending navbar
$nav = $('div.navbox')
// check if the navbar is fixed
if ( $nav.css('position') !== "fixed" ) return
// listen for when the browser performs the scroll
$(window).one('scroll', function () {
// scroll the window up by the height of the navbar
window.scrollBy(0, -$nav.height())
});
});
});
</script>
有人能指出我的实现中的错误吗?结果是调用了其他我不知道的函数。谢谢Jason的提示。这些脚本确保了平滑滚动,这是我使用的模板类型的一个常见特性,因此在这里给出我的解决方案可能会很有用 最后,我通过在现有函数中添加几行代码来解决这个问题,该函数与我自己尝试实现的函数类似 以下是新代码,我在其中添加了注释代码:
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
/* added by me: */
var $x;
var $nav;
$nav = $('nav#tf-menu')
if ( $nav.css('position') == "fixed" ){ $x=0; } else {$x=$nav.height()+30};
/* end added */
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - 70 - $x //added the -$x
}, 1000);
return false;
}
}
});
}
您从未添加单击事件。。。检查语法。我只是复制/跳过这些脚本,无法正确编写它们。但根据非我的评论,这应该选择链接上的所有点击事件,href以?但我想它不会这么做。有什么建议吗?它写得像.onclick,a[href^=],function e{谢谢!现在函数被调用了,但不是为。在这些查询中是否有类似于通配符的东西?实际上…您的初始代码在fiddle中运行得非常好。是否有冲突的脚本?确保您的脚本是在关闭body标记之前编写的,在所有插件脚本之后编写的。我的意思是说类似于[您的脚本]