Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 冲突脚本无法运行_Javascript_Jquery - Fatal编程技术网

Javascript 冲突脚本无法运行

Javascript 冲突脚本无法运行,javascript,jquery,Javascript,Jquery,我在页脚上声明了两个脚本。每个脚本在单独声明时都能完美工作,但如果我声明了第二个脚本。另一个功能是另一个功能不是 下面是单独声明并与其他脚本一起声明时可以使用的代码。 这有他自己的js文件 var fixed = false; $(document).scroll(function() { if( $(this).scrollTop() > 20 ) { if( !fixed ) { fixed = true; $(

我在页脚上声明了两个脚本。每个脚本在单独声明时都能完美工作,但如果我声明了第二个脚本。另一个功能是另一个功能不是

下面是单独声明并与其他脚本一起声明时可以使用的代码。 这有他自己的js文件

var fixed = false;

$(document).scroll(function() {
    if( $(this).scrollTop() > 20 ) {
        if( !fixed ) {
            fixed = true;
            $('.navbar-fixed-top').addClass('scroll');

        }
    } else {
        if( fixed ) {
            fixed = false;
            $('.navbar-fixed-top').removeClass('scroll');

        }
    }
});

/*
 * Smooth Scroll
 */


$(function() {

        function filterPath(string) {
            return string
            .replace(/^\//,'')
            .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
            .replace(/\/$/,'');
        }

        var locationPath = filterPath(location.pathname);
        var scrollElem = scrollableElement('html', 'body');

        // Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
        $('a[href*=#]').each(function() {

            // Ensure it's a same-page link
            var thisPath = filterPath(this.pathname) || locationPath;
            if (  locationPath == thisPath
                && (location.hostname == this.hostname || !this.hostname)
                && this.hash.replace(/#/,'') ) {

                    // Ensure target exists
                    var $target = $(this.hash), target = this.hash;
                    if (target) {

                        // Find location of target
                        var targetOffset = $target.offset().top;
                        $(this).click(function(event) {

                            // Prevent jump-down
                            event.preventDefault();

                            // Animate to target
                            $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {

                                // Set hash in URL after animation successful
                                location.hash = target;

                            });
                        });
                    }
            }

        });

        // Use the first element that is "scrollable"  (cross-browser fix?)
        function scrollableElement(els) {
            for (var i = 0, argLength = arguments.length; i <argLength; i++) {
                var el = arguments[i],
                $scrollElement = $(el);
                if ($scrollElement.scrollTop()> 0) {
                    return el;
                } else {
                    $scrollElement.scrollTop(1);
                    var isScrollable = $scrollElement.scrollTop()> 0;
                    $scrollElement.scrollTop(0);
                    if (isScrollable) {
                        return el;
                    }
                }
            }
            return [];
        }

    });
var fixed=false;
$(文档)。滚动(函数(){
如果($(this).scrollTop()>20){
如果(!固定){
固定=正确;
$('.navbar固定顶').addClass('scroll');
}
}否则{
如果(固定){
固定=假;
$('.navbar固定顶').removeClass('scroll');
}
}
});
/*
*平滑卷轴
*/
$(函数(){
函数筛选器路径(字符串){
返回字符串
.替换(/^\/,'')
.replace(/(索引|默认值)。[a-zA-Z]{3,4}$/,“”)
.替换(/\/$/,'');
}
var locationPath=filterPath(location.pathname);
var scrolleem=scrollableElement('html','body');
//其中包含哈希标记的任何链接(无法执行^=操作,因为具有完全限定的URL潜力)
$('a[href*=#]')。每个(函数(){
//确保它是同一个页面链接
var thisPath=filterPath(this.pathname)| | locationPath;
如果(locationPath==此路径
&&(location.hostname==this.hostname | |!this.hostname)
&&this.hash.replace(/#/,''){
//确保目标存在
var$target=$(this.hash),target=this.hash;
如果(目标){
//找到目标的位置
var targetOffset=$target.offset().top;
$(此)。单击(函数(事件){
//防跳
event.preventDefault();
//为目标设置动画
$(scrollElem).animate({scrollTop:targetOffset},400,function(){
//动画成功后在URL中设置哈希
location.hash=目标;
});
});
}
}
});
//使用第一个“可滚动”的元素(跨浏览器修复?)
函数可滚动元素(els){
for(var i=0,argLength=arguments.length;i 0){
返回el;
}否则{
$scrollElement.scrollTop(1);
变量isScrollable=$scrollElement.scrollTop()>0;
$scrollElement.scrollTop(0);
如果(可循环使用){
返回el;
}
}
}
返回[];
}
});
另一个是在第一个脚本下面声明的。现在,当另一个人在场时,这一点不起作用,但在独自一人时,这一点非常有效。我还尝试了无冲突脚本,但结果也是一样

<script>
$(document).ready(function(){

$(function(){
   document.querySelector( "#nav-toggle" )
    .addEventListener( "click", function() {
      this.classList.toggle( "active" );
      $(".navmobile").slideToggle();
 });
});
      $(window).resize(function() {
      if ($(window).width() >= 767) {
         $(".navmobile").hide();
      }
      else {
    $("#nav-toggle").removeClass("active");
    $(".navmobile").hide();
    }
   });
});
</script>

$(文档).ready(函数(){
$(函数(){
文档查询选择器(“导航切换”)
.addEventListener(“单击”,函数(){
this.classList.toggle(“活动”);
$(“.navmobile”).slideToggle();
});
});
$(窗口)。调整大小(函数(){
如果($(窗口).width()>=767){
$(“.navmobile”).hide();
}
否则{
$(“#导航切换”).removeClass(“活动”);
$(“.navmobile”).hide();
}
});
});

您的代码中可能包含以下元素:

<a href="#" id="nav-toggle"></a>

请注意
href=“#”
部分-第一个脚本使用
event.preventDefault()
对每个href包含“#”的A元素,因此第二个脚本不会获得click事件从该元素中删除
href=“#”
(或将属性保留为空),它应该可以工作。

更改

// Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
$('a[href*=#]').each(function() {


因此,第一个脚本忽略了您单击以进行切换的链接。

“另一个函数另一个函数不是。”我不知道这意味着什么。脚本不能一起工作。一个工作,另一个不工作。当两个函数都被声明时,只有一个函数在工作。所以您需要找出原因。似乎两者都在处理锚定,所以我猜您需要排除另一个正在使用的锚定。当我单击导航切换功能时,我的favicon.ico上出现了内部服务器错误。基本上,它应该只在锚点上添加一个类。不知道为什么控制台在favicoyes上出现错误我有那个代码
但是删除href并不能解决任何问题
// Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
$('a[href*=#]').not("#av-toggle").each(function() {