我在数据属性内有json。在诸如http://jsonlint.com/的在线验证器中有效。仍然引发错误。怎么了?
的HTML
<div class="seo" data-seo-controller='{"values":{"child_count":"0","type":"tables"},"title":{"field":"\u00e5\u00e4\u00f6 llasdas","template":"This page has {{child_count}} {{type}}!","prefix":"","suffix":" - Products","fallback":"\u00e5\u00e4\u00f6 llasdas","full":"\u00e5\u00e4\u00f6 llasdas - Products","full-replaced":"\u00e5\u00e4\u00f6 llasdas - Products"},"description":{"field":"rwerwe wer " ' ewrerte fsd :'","fallback":"rwerwe wer " ' ewrerte fsd :'","full":"rwerwe wer " ' ewrerte fsd :'","full-replaced":"rwerwe wer " ' ewrerte fsd :'","template":"","prefix":"","suffix":"","limit":155},"url":{"edit":"http:\/\/localhost\/seo\/panel\/pages\/saved-value\/url","preview":"seo\/saved-value"}}'>
jQuery的
$( document ).ready(function() {
var json = $('.seo').attr('data-seo-controller');
controller = jQuery.parseJSON( json );
console.log(controller);
});
小提琴
https://jsfiddle.net/q89hph0L/
未捕获到的SyntaxError:意外令牌'
更新:
如果复制将json粘贴到字符串中并进行解析,则似乎可行。更新的小提琴:https : //jsfiddle.net/q89hph0L/4/
因此,它不能将json作为数据属性吗?
"
由浏览器呈现到中"
,请参阅:如何在title属性中转义双引号。与您的用例相同。
解决方案是将替换"
为\"
,使用时jQuery.data
甚至不必使用解析字符串JSON.parse()
。更新了jsfiddle:https ://jsfiddle.net/0qryjrya/ 。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句