【问题标题】:.substring error: "is not a function".substring 错误:“不是函数”
【发布时间】:2011-05-09 01:13:33
【问题描述】:

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

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

网站: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 不是函数”

但这部分代码没问题:

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.

【问题讨论】:

    标签: javascript jquery substring


    【解决方案1】:

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

    解决方法的捷径:document.location.toString().substring(2,3);

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

    【讨论】:

    • 当我使用toString()converts 我的字符串到[object Object] (我在字符串中的任何东西,都被替换)。你知道怎么解决吗?
    • @ChristopherAlatorre 取决于您要字符串化的对象。如果它是一个没有明显过载的对象,您将始终得到 [object Object] 结果。欲了解更多信息,请参阅:stackoverflow.com/questions/27649368/…
    【解决方案2】:

    你也可以引用字符串

    ''+document.location+''.substring(2,3);
    

    【讨论】:

      【解决方案3】:

      您可以使用substr

      例如:

      new Date().getFullYear().toString().substr(-2)
      

      【讨论】:

      • 我有 toString().substr(-2) 部分,但是我需要 npm install 吗?
      • 不,不需要这是纯javascript函数
      【解决方案4】:

      试试下面的代码:

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

      【讨论】:

        【解决方案5】:

        我正在讨论这篇文章的主题,因为我们遇到了类似的问题。一段我们似乎无法修复的代码。这是我们的代码

        function addLink() {
            var e,
                n = document.getElementsByTagName("body")[0],
                t = (e = window.getSelection()) + ("<a href='" + document.location.href + "'>.</a>"),
                o = document.createElement("div");
            (o.style.position = "absolute"),
                (o.style.left = "-99999px"),
                n.appendChild(o),
                (o.innerHTML = t),
                e.selectAllChildren(o),
                window.setTimeout(function () {
                    n.removeChild(o);
                }, 0);
        }

        我们必须先删除变量 T 的最后一个字符,然后再用 href 标记添加点 但是如果我们使用子字符串函数,就会出现“子字符串不是函数”错误

        问候

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 2021-10-12
        • 2016-01-25
        • 2013-10-13
        • 1970-01-01
        • 2016-06-09
        • 1970-01-01
        • 2017-10-23
        相关资源
        最近更新 更多