Making a variable in jquery 1.9.1 fails and in 1.8.3 doesn't

雨燕双飞 提交于 2019-12-11 04:06:38

问题


I just upgraded to 1.9.1 After some technical defiificulties all work except:

var $newthumbs = $('                <div id=\"car-7\" class=\"thumbnail_car thumbnail span2\" data-sorting-top=\"2\" data-sorting-date=\"2013-01-12 16:47:31 UTC\"></div>');

If I put this line in the console of jquery 1.8.3 it gets accepted and I can retrieve it with $newthumbs

in 1.9.1 it fails with Error: Syntax error, unrecognized expression:

I've read the changelog and I don't see anything relating that should break this. I may not know a lot of jquery, but this type of syntax looks standard.

What changed?

Update

I did find this here

HTML strings with leading whitespace: jQuery 1.9 restricts the strings processed by $() for security reasons. Although we recommend you use $.parseHTML() to process arbitrary HTML like templates, the 1.1.0 version of the Migrate plugin restores the old behavior.


回答1:


If you remove the spaces, it will work:

var $newthumbs = $('<div id=\"car-7\" class=\"thumbnail_car thumbnail span2\" data-sorting-top=\"2\" data-sorting-date=\"2013-01-12 16:47:31 UTC\"></div>');

DEMO: http://jsfiddle.net/dirtyd77/KFmMQ/1/




回答2:


Your problem are the whitespaces in the beginning of the string. I have no idea why this is a problem, but it obviously is.

By the way: You can remove the pointless backslashes:

var $newthumbs = $('<div id="car-7" class="thumbnail_car thumbnail span2" data-sorting-top="2" data-sorting-date="2013-01-12 16:47:31 UTC"></div>');

or you can use double quotation marks for the surrounding string:

var $newthumbs = $("<div id=\"car-7\" class=\"thumbnail_car thumbnail span2\" data-sorting-top=\"2\" data-sorting-date=\"2013-01-12 16:47:31 UTC\"></div>");


来源:https://stackoverflow.com/questions/14817039/making-a-variable-in-jquery-1-9-1-fails-and-in-1-8-3-doesnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!