运行 jQuery 到控制台的无效或意外令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37794465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:08:20  来源:igfitidea点击:

Invalid or unexpected token running jQuery to console

jquery

提问by Stavros B

I try to do this with jQuery:

我尝试用 jQuery 做到这一点:

I have an id with a specific class. When scrolling this class changes to another class. So, i need to check if this id has the specific class then to add class to another id.

我有一个特定类的 id。滚动此类更改为另一个类时。所以,我需要检查这个 id 是否具有特定的类,然后将类添加到另一个 id。

This is what i tried directly through Console of Chrome but i get Uncaught SyntaxError: Invalid or unexpected token.

这是我直接通过 Chrome 控制台尝试的,但我得到了 Uncaught SyntaxError: Invalid or unexpected token

if (jQuery(#header).hasClass("affix") ) {
  jQuery(#logo).addClass("otherlogo");
}

回答by Praveen Kumar Purushothaman

That's an invalid syntax. You need to add quotes around the selectors, as they are strings. Change it this way:

这是一个无效的语法。您需要在选择器周围添加引号,因为它们是字符串。改成这样:

if (jQuery("#header").hasClass("affix")) {
  jQuery("#logo").addClass("otherlogo");
}

The #headerand #logoshould be strings.

#header#logo应弦。

回答by terpinmd

You're missing quotes:

你缺少引号:

if (jQuery("#header").hasClass("affix") ) {
  jQuery("#logo").addClass("otherlogo");
}