我找到了一个解决方案,而不是用这个替换空格%0d%0a
从这个
Line break inside HTML tag attribute value
var base64 =
{
encode: function utoa(str)
{
return window.btoa(unescape(encodeURIComponent(str)));
},
decode: function atou(str)
{
return decodeURIComponent(escape(window.atob(str)));
}
}
我试过了,它也能工作,它也使字符串不可读,因为它是base64_编码的,它避免了空格和引号引起的换行。
var $json = base64.encode(JSON.stringify('{"Long_text":"This is \'my json string and soon..."}'));
$("#button_id").attr('data-json', $json);
然后获取值并再次转换,
var valid_json = JSON.parse(base64.decode($("#button_id").attr('data-json')));
谢谢!