简体   繁体   English

语法错误:缺少元素列表后的[]([对象对象])

[英]SyntaxError: missing ] after element list ([object Object])

I return JSON from PHP, it works fine, but when the data is empty i fill in default JSON data, to use for applying .css on a jQuery selected element, like this: 我从PHP返回JSON,效果很好,但是当数据为空时,我填写默认的JSON数据,用于在jQuery选定元素上应用.css,如下所示:

db.data = { "blocks" :  {"css" : {"background-color" : "purple" }}};

(Edit: the css from my db actually contains slashes and does work with the eval() : "css":"{ \\"background-color\\" : \\"grey\\" } ) (编辑:我数据库中的css实际上包含斜杠,并与eval()一起使用: "css":"{ \\"background-color\\" : \\"grey\\" } ))

If I console.log it, FF will display it as an object (all green and red and nicely formatted and clickable/expandable in firebug: 如果我进行console.log记录,FF会将其显示为一个对象(所有绿色和红色以及格式正确且可在Firebug中单击和扩展):

Object { background-color= "purple" }

I eval() it before passing it to css. 我将它传递给CSS之前先eval() I tried $.evalJSON from the jQuery JSON Plugin, but it will give: 我从jQuery JSON Plugin尝试了$.evalJSON ,但是它将给出:

SyntaxError: JSON.parse: unexpected character

Edit: I tried it without eval() but will throw the same error, but not when I use the css with the slashes 编辑:我没有eval()尝试过,但会抛出相同的错误,但是当我使用带有斜杠的CSS时不会

I tried manually adding a ] somewhere, but obviously wont work, and if I add two, it will create an array, which I don't want. 我尝试在某处手动添加一个[ ] ,但显然无法正常工作,如果添加两个,它将创建一个数组,这是我不想要的。

I apply CSS to element like this: 我将CSS应用于这样的元素:

DOMID = $('#target');
var CSS = eval(" (" +db.data.blocks.css+") ");
this.DOMID.css(CSS);

Tried without the " (" , without spaces, "(" . 尝试不带" (" ,不带空格"("

any ideas? 有任何想法吗?

The code you posted would never work because it is misguided. 您发布的代码将永远无法使用,因为它被误导了。 There is no need to eval anything, and even if there were coercing db.data.blocks.css to a string (in order to put in the parens) would give you back the completely wrong value. 无需eval任何内容,即使将db.data.blocks.css转换为字符串(以便放入括号)也将带给您完全错误的值。

Instead, you should simply write 相反,您应该简单地写

this.DOMID.css(db.data.blocks.css);

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

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