实现不了啊亲,卡在这里好久了,哪里出错了呢~

来源:6-4 编程练习

正在睡觉

2017-04-09 00:21

<script type="text/javascript">
        
        function hasClass( obj, cls ){
            return obj.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
        }

        function removeClass( obj, cls ){ 
        if( hasClass( obj, cls )){ 
            
            var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
            obj.className = obj.className.replace(reg, "");
        }
    }
        function addClass( obj, cls ){
            if(!hasClass(obj,cls)){
                obj.className = " current";
            }
            // obj.className = " current";
        }

        //根据CLASSname获取元素
        // function getByClassName( obj, cls ){
        //     var elenemts = obj.getElementsByTageName("*");
        //     alert(elements);
        //     var result = [];
        //     for(var i = 0 ; i < elenemts.length; i++){
        //         if(elenemts[i].className == cls){
        //             result.push(elements[i]);
        //         }
        //     }
        //     return result;
            
        // }
		
        window.onload = function() {
        
            window.onscroll = function(){
            	
                var top = document.body.scrollTop || document.documentElement.scrollTop; 
               
               
          
                var menus = document.getElementById("menu").getElementsByTagName("a");
                
                var items = document.getElementById("content").getElementsByClassName("item");

                
                var currentId = "";
                
                for ( var i=0; i< items.length; i++ ){ 
                    var _item = items[i];
                    var _itemTop = _item.offsetTop;

                    // console.log(top);
                    // console.log(_itemTop-200);
                    if( top > _itemTop - 200){ 
                        currentId = _item.id;
                        // alert(currentId);

                    } else { 
                        break;
                    }  
               }         
               

                if(currentId){
                    //给正确A元素加CLASS
                    for(var i = 0; i < menus.length; i++){
                        var _menu = menus[i];
                         var _href = _menu.href.split("#");
                       // console.log(_href);

                        if(_href[_href.length-1] == currentId){
                        	 addClass(_menu,"current");
                           
                           
                        }
                        else{
                            removeClass(_menu,"current");
                           // alert(_menu.className);
                        }
                    }
                }
            }

        }
    </script>


写回答 关注

2回答

  • 慕粉1711228114
    2017-04-11 17:59:10
    已采纳

    如果你是在做练习题,还要检查把html里面的menu下面a的href="#"对应锚点补充上去,

    给最佳啊,积分也不知道拿来干嘛的

  • 慕粉1711228114
    2017-04-11 17:36:42

    getByClassName()这个函数被注释掉了,取消注释然后var items = document.getElementById("content").getElementsByClassName("item");替换为

    var items = getByClassName(document.getElementById("content"), "item");

    正在睡觉

    是啊,突然发现点锚都不能跳转,然后就加上,然后就好了

    2017-04-12 00:25:06

    共 2 条回复 >

网页定位导航特效

本课程讲解网页定位导航特效,仿天猫版地狗购物网,你值得拥有

71404 学习 · 486 问题

查看课程

相似问题