简体   繁体   English

预期的表达方式,是否有'}'?

[英]expected expression, got '}'?

I am trying to pass the following content into a jquery variable but when I click on it it says expected expression, got }. 我正在尝试将以下内容传递到jquery变量中,但是当我单击它时,它说了预期的表达式,得到了}。 It has to do with the quotation right? 与报价有关吗? How am I supposed to use double and single quotes in this instance? 在这种情况下,我应该如何使用双引号和单引号?

var content = $("<div class='channel' id='general' onclick='jumpToFirstLevelRoom('general')'>General</div>")

Yes your quotes are wrong. 是的,您的报价有误。 You have unescaped single quotes inside single quotes. 您在单引号内未转义单引号。

You should use either of these quotation techniques : 您应该使用以下两种报价技术之一:

  • Different quotes : 不同的引号:

     var content = $("<div class='channel' id='general' onclick='jumpToFirstLevelRoom(`general`)'>General</div>") 
  • Escaped quotes : 引号:

     var content = $("<div class='channel' id='general' onclick='jumpToFirstLevelRoom(\\'general\\')'>General</div>") 

您可以使用转义序列从变量中删除双引号。

var content = $("<div class=\"channel\" id=\"general\" onclick=\"jumpToFirstLevelRoom('general')\">General</div>")

Change your current code 更改当前代码

var content = $("<div class='channel' id='general' onclick='jumpToFirstLevelRoom('general')'>General</div>")

to

var content = $("<div class='channel' id='general' onclick='jumpToFirstLevelRoom(\'general\')'>General</div>");

Just use escape characters . 只需使用escape characters

With es6 you can do 使用es6,您可以

var content = $(`<div class='channel' id='general' onclick='jumpToFirstLevelRoom("general")'>General</div>`);

Hope this will solve the problem. 希望这能解决问题。

What you are looking for is this: 您正在寻找的是:

var content = $("<div class='channel' id='general' enter code hereonclick='jumpToFirstLevelRoom(\'general\')'>General</div>");

You need to use escape characters to remove double quotes. 您需要使用转义符删除双引号。

I got the problem in your code. 我在您的代码中遇到了问题。 The problem is in that statement where you are calling a function and passing arguments. 问题出在您正在调用函数并传递参数的那条语句中。

var content = $("<div class='channel' id='general' onclick='jumpToFirstLevelRoom('general')'>General</div>")

You are passing a string general in function jumpToFirstLevelRoom('general') . 您要在函数jumpToFirstLevelRoom('general')中传递一般字符串。 So when you are starting single quotes of string general , It is behaving as closing quote of onclick=' . 因此,当您启动字符串general的单引号时,它表现为onclick ='的结束引号。

Try to not pass the string in the function as argument. 尽量不要在函数中将字符串作为参数传递。 Then the code will run. 然后代码将运行。 I hope it will help you. 希望对您有帮助。 :) :)

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

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