.substring错误:“不是函数"; [英] .substring error: "is not a function"

查看:159
本文介绍了.substring错误:“不是函数";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我使用substring方法声明变量时收到错误消息.

I don't understand why I get an error message using the substring method to declare a variable.

我想在比较中使用URL的第一部分.

I want to use the first part of the URL in a comparison.

站点: http://www.elizabet.nl/wordpress

这是出问题的部分:

var currentLocation = document.location,
muzLoc = currentLocation.substring(0,45),
prodLoc = currentLocation.substring(0,48), 
techLoc = currentLocation.substring(0,47); 

错误: "currentLocation.substring不是函数"

The error: "currentLocation.substring is not a function"

但是这部分代码很好:

var URL = $(this).attr("href").substring(2) + ' #main';

所有代码:

jQuery(function($){

    var siteURL = "http://" + top.location.host.toString() + "/wordpress", // Declareren van URL van de website.
        URL = '', // Declareren van een URL, welke dan ook. 
        currentLocation = '',
        muzLoc = '',
        prodLoc = '',
        techLoc = '',               
        allLinks = $('a[href^=' + siteURL + ']' ), // Declareren van alle menu-links. Het teken ^ betekent 'begint met'.
        otherLinks = $('a[href^=' + siteURL + "/wp-content" + ']'), 
        siteLinks = $(allLinks).not(otherLinks),      
        mainDiv = $("#content"),
        hash = window.location.hash,
        muziekURL = "http://www.elizabet.nl/wordpress/#/muziek_pf/",
        productieURL = "http://www.elizabet.nl/wordpress/#/productie_pf/",
        techniekURL = "http://www.elizabet.nl/wordpress/#/techniek_pf/";    

    if (hash) {
        hash = "/wordpress" + hash.substring(1); // substring methode haalt karakters van je string af. In dit geval de #, vanwege de offset=1.
        URL = hash;
        $(mainDiv).load(URL);           
    }

function pageLoad() {

                var allLinks = $('a[href^=' + siteURL + ']' ),
                otherLinks = $('a[href^=' + siteURL + "/wp-content" + ']'), 
                siteLinks = $(allLinks).not(otherLinks); 

                siteLinks.each(function() {             

                    $(this).attr("href", "#" + this.pathname.substring(10));
                    })

                    .click(function() {
                    var URL = $(this).attr("href").substring(2) + ' #main';
                    $(mainDiv).load(URL, function(){ 

                    var currentLocation = document.location,
                        muzLoc = currentLocation.substring(0,45),
                        prodLoc = currentLocation.substring(0,48), 
                        techLoc = currentLocation.substring(0,47);                  

                if (muzLoc == muziekURL) {              
                $("body").animate({ backgroundColor: "#151C07"}, 500);
                $(".nieuws").animate({ borderBottomColor: "#99CC33"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#99CC33"}, 500);
                $("#tabtekst_3").stop().animate({ backgroundColor: "#B8860B" }, 500);
                $("#tab_3").add("a.gold").stop().animate({ color: "#B8860B" }, 500);
                $("#tabtekst_4").stop().animate({ backgroundColor: "#765AAD" }, 500);  
                $("#tab_4").add("a.purple").stop().animate({ color: "#765AAD" }, 500);                                    
                }

                else if (prodLoc == productieURL) {     
                $("body").animate({ backgroundColor: "#251B02"}, 500);  
                $(".nieuws").animate({ borderBottomColor: "#FFCC33"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#FFCC33"}, 500);                  
                $("#tabtekst_2").stop().animate({ backgroundColor: "#6B8E23" }, 500);  
                $("#tab_2").add("a.green").stop().animate({ color: "#6B8E23" }, 500);   
                $("#tabtekst_4").stop().animate({ backgroundColor: "#765AAD" }, 500);  
                $("#tab_4").add("a.purple").stop().animate({ color: "#765AAD" }, 500);
                } 

                else if (techLoc == techniekURL) {      
                $("body").animate({ backgroundColor: "#181223"}, 500);  
                $(".nieuws").animate({ borderBottomColor: "#B39BE4"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#B39BE4"}, 500);          
                $("#tabtekst_2").stop().animate({ backgroundColor: "#6B8E23" }, 500);  
                $("#tab_2").add("a.green").stop().animate({ color: "#6B8E23" }, 500);                       
                $("#tabtekst_3").stop().animate({ backgroundColor: "#B8860B" }, 500);
                $("#tab_3").add("a.gold").stop().animate({ color: "#B8860B" }, 500);
                } 

                else {
                $("body").animate({ backgroundColor: "#202020"}, 500);  
                $(".nieuws").animate({ borderBottomColor: "#FFF"}, 500);  
                $("#stripe_trans").add("#header").animate({ backgroundColor: "#FFF"}, 500);             
                $("#tabtekst_2").stop().animate({ backgroundColor: "#6B8E23" }, 500);  
                $("#tab_2").add("a.green").stop().animate({ color: "#6B8E23" }, 500);               
                $("#tabtekst_3").stop().animate({ backgroundColor: "#B8860B" }, 500);
                $("#tab_3").add("a.gold").stop().animate({ color: "#B8860B" }, 500);
                $("#tabtekst_4").stop().animate({ backgroundColor: "#765AAD" }, 500);  
                $("#tab_4").add("a.purple").stop().animate({ color: "#765AAD" }, 500);                                          
                }

                pageLoad();
            });             
        });
}

pageLoad();


}); // End document ready function.

推荐答案

document.location是一个对象,而不是字符串.它返回(默认情况下)完整路径,但实际上包含的信息更多.

document.location is an object, not a string. It returns (by default) the full path, but it actually holds more info than that.

解决方案的快捷方式:document.location.toString().substring(2,3);

Shortcut for solution: document.location.toString().substring(2,3);

或使用document.location.hrefwindow.location.href

这篇关于.substring错误:“不是函数";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆