简体   繁体   English

jquery“包含”代码不适用于chrome

[英]jquery “contains” code doesn't work on chrome

I'm using this code to check what language is on the site and then remove it from the my dropdown menu. 我正在使用此代码检查网站上的语言,然后从我的下拉菜单中删除它。 The code works in Firefox but fails to work on chrome and stops all other scripts as well. 该代码适用于Firefox,但无法在chrome上运行并停止所有其他脚本。 This is the code: 这是代码:

var mylangme = $(location).attr('href');

if(mylangme.contains("/fr/")){
    mylangme="French";
    $(".subnav li:first-child").css("display","none");
                    }
if(mylangme.contains("/nl/")){
    mylangme="Dutch";
    $(".subnav li:nth-of-type(2)").css("display","none");
                    }
if(mylangme.contains("/ru/")){
        mylangme="Russian";
        $(".subnav li:nth-of-type(3)").css("display","none");
                    }
if(mylangme.contains("/en/")){
        mylangme="English";
        $(".subnav li:last-child").css("display","none");
                    }   

@Quentin is right, you are using a jQuery method on a non-jQuery object. @Quentin是对的,你在非jQuery对象上使用jQuery方法。 You can fix it using the indexOf method that is part of the standard JavaScript library, and as such is supported by all browsers. 您可以使用作为标准JavaScript库一部分的indexOf方法来修复它,因此所有浏览器都支持此方法。 The indexOf method will return -1 if the string was not found. 如果找不到字符串, indexOf方法将返回-1 Your code would then look like this: 您的代码将如下所示:

if(mylangme.indexOf("/fr/") != -1) {
    mylangme="French";
    $(".subnav li:first-child").css("display","none");
}

That isn't jQuery. 那不是jQuery。 The attr method returns a String, which is core JavaScript. attr方法返回一个String,它是核心JavaScript。

The contains method for Strings was introduced in JavaScript 1.9 and is only supported by Firefox. Strings的contains方法是在JavaScript 1.9中引入的,只有Firefox支持。

Use indexOf or the polyfill given on the (above linked) MDN documentation page. 使用indexOf或(上面链接的)MDN文档页面上给出的polyfill。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM