javascript – Console.log根本不工作

前端之家收集整理的这篇文章主要介绍了javascript – Console.log根本不工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一堆代码不起作用,我试图确定问题所在,但是console.log()没有在Chrome开发工具中记录任何结果,我正在做正确的操作?
$(window).scroll(function() {
       $('section').each(function(){
            var id='#'+$(this).attr('id'),off=$(id).offset().top,hei=$(id).height(),winscroll=$(window).scrollTop(),dif=hei+off-($(window).height());

            if (winscroll >= off && winscroll<=dif) {
                console.log('first broken');
                $(id+' .sticky').removeClass('abs').addClass('fix');
            } else if (winscroll > dif){
                console.log('second broken');
                $(id+' .sticky').removeClass('fix').addClass('abs');
            } else {
                console.log('third broken');
                $(id+' .sticky').removeClass('fix abs');
            }   });
        });

编辑完整代码添加

$(document).ready(function() {

    // If a browser supports 3D transforms use the fancy menu if it doesn't,use standard accordion menu instead
    if($('html').hasClass('csstransforms3d')){

        $( "#mp-menu" ).removeClass( "snap-drawers" ).addClass( "mp-menu" );

        $('nav ul li ul').css('border-bottom','1px solid rgba(255,255,.05)');
        $('nav ul li ul').css('background','none');

        // Insert elements where necessary to create the right structure
        $('#mp-menu').wrapInner('<div class="mp-level" />');
        $('#mp-menu').find('li > ul').wrap('<div class="mp-level" />');

        $("#mp-menu ul li .mp-level").prepend(function () {
            return '<span class="menu-title">' + $(this).prev().text() + '</span> <a class="ico mp-back" href="#">Back</a>';
        });

        // load in necessary JS files
        $.getScript('http://176.32.230.2/baodev.com/cjo/wp-content/themes/CJO/js/multi-level-menu.js');

    } else {

        // load in necessary JS files
        $.getScript( "http://176.32.230.2/baodev.com/cjo/wp-content/themes/CJO/js/jquery.navgoco.min.js",function() {
            $("#demo1").navgoco({accordion: true});
        });

        $.getScript( "http://176.32.230.2/baodev.com/cjo/wp-content/themes/CJO/js/snap.min.js",function() {

            // Snapper settings     
            var snapper = new Snap({
              element: document.getElementById('scroller'),disable: 'right',maxPosition: 291
            });

            var addEvent = function addEvent(element,eventName,func) {
                if (element.addEventListener) {
                return element.addEventListener(eventName,func,false);
              } else if (element.attachEvent) {
                  return element.attachEvent("on" + eventName,func);
              }
            };

            // Toggle button
            addEvent(document.getElementById('trigger'),'click',function(){
                if( snapper.state().state=="left" ){
                    snapper.close();
                    $( ".menu-trigger" ).removeClass( "active" );
                } else {
                    snapper.open('left');
                    $( ".menu-trigger" ).addClass( "active" );
                }
            });

            addEvent(document.getElementById('scroller'),function(){
                if( snapper.state().state=="left" ){
                    $( ".menu-trigger" ).removeClass( "active" );
                }
            });

            /* Prevent Safari opening links when viewing as a Mobile App */
            (function (a,b,c) {
              if(c in b && b[c]) {
                  var d,e = a.location,f = /^(a|html)$/i;
                  a.addEventListener("click",function (a) {
                      d = a.target;
                      while(!f.test(d.nodeName)) d = d.parentNode;
                      "href" in d && (d.href.indexOf("http") || ~d.href.indexOf(e.host)) && (a.preventDefault(),e.href = d.href)
                  },!1)
              }
            })(document,window.navigator,"standalone");

        });

    } // end if

    fitHeight();

    $(window).scroll(function() {
        $('section').each(function(){
            var id='#'+$(this).attr('id'),dif=hei+off-($(window).height());

           console.log('msj');

            if (winscroll >= off && winscroll<=dif) {
                $(id+' .sticky').removeClass('abs').addClass('fix');
            } else if (winscroll > dif){
                $(id+' .sticky').removeClass('fix').addClass('abs');
            } else {
                $(id+' .sticky').removeClass('fix abs');
            }
        });
     });

});

// Trigger FitHeight on browser resize
$(window).resize(fitHeight);

编辑

完整代码的一些位(上)指的是其他JS文件,并且运行时出现这些文件代码返回错误.排除故障之后,在滚动功能之前看到控制台消息,但是在滚动功能中看不到控制台消息.

fitHeight();

    console.log('About to bind scroll effects'); // I SEE THIS MESSAGE

    $(window).scroll(function() {

        console.log("scroll bound,now loop through sections"); //BUT NOT THIS ONE

        $('section').each(function(){

解决方法

听起来像您已经隐藏了JavaScript日志或指定您只想看到错误或警告.打开Chrome的开发者工具,然后转到控制台选项卡.在底部您想要确保JavaScript被勾选,并确保您选择了“所有”,“日志”或“调试”.

在上图中,我有JavaScript,网络,日志记录,CSS和其他勾选,并选择“全部”.

另一个潜在的问题可能是您的$(窗口).scroll()函数不包含在.ready()函数中(如文档here所示):

$(document).ready(function() {
    $(window).scroll(function() {
        ...
    });
});

代码粘贴到JSFiddle并给出一些虚拟内容时,您的代码工作完全正常:JSFiddle demo.

编辑:

问题已编辑.给出的新代码抛出两个错误

Uncaught ReferenceError: fitHeight is not defined
Uncaught TypeError: Cannot read property ‘addEventListener’ of null

因此,代码在到达任何console.log调用之前停止执行.

猜你在找的JavaScript相关文章