我使用jquery来获取隐藏输入的值。隐藏输入包含带有JSON对象的data-trix-attachment属性。问题是jquery用"
替换了引号。我不知道为什么。
我已经四处寻找解决方案了,似乎无法找到解决方案。
我尝试使用正则表达式将"
替换为',但这也不起作用。 (下面是一个例子)
任何人都可以帮我解决这个问题。以下是我尝试使用的代码。
var new1 = $("#trix-input-1").attr("value");
var e_encoded = new1.replace(/"/g, """);
HTML代码
<a contenteditable="false" href="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png" data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2015-11-16 at 11.36.45 AM.png","filesize":9291,"height":77,"href":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","url":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","width":305}" data-trix-content-type="image/png" data-trix-id="38">
jQuery响应
<a data-trix-content-type="image/png" data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2015-11-16 at 11.36.45 AM.png","filesize":9291,"height":77,"href":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","url":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","width":305}" href="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png"><figure class="attachment attachment-preview png"><img src="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png" height="77" width="305"><figcaption class="caption">Screen Shot 2015-11-16 at 11.36.45 AM.png <span class="size">9.07 KB</span></figcaption></figure></a>
jQuery代码
var new1 = $("#trix-input-1").val();
答案 0 :(得分:2)
replace
的语法错误,它的replace(oldStr, newStr)
反之亦然。见这里:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace
所以这个:
var e_encoded = new1.replace(/"/g, """);
应该是:
var e_encoded = new1.replace(/"/g, "'");
你的例子很好用:
console.log('<a data-trix-content-type="image/png" data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2015-11-16 at 11.36.45 AM.png","filesize":9291,"height":77,"href":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","url":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","width":305}" href="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png"><figure class="attachment attachment-preview png"><img src="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png" height="77" width="305"><figcaption class="caption">Screen Shot 2015-11-16 at 11.36.45 AM.png <span class="size">9.07 KB</span></figcaption></figure></a>'.replace(/"/g, "'"))
&#13;
使用document.write而不是console output:
document.write('<a data-trix-content-type="image/png" data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2015-11-16 at 11.36.45 AM.png","filesize":9291,"height":77,"href":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","url":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","width":305}" href="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png"><figure class="attachment attachment-preview png"><img src="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png" height="77" width="305"><figcaption class="caption">Screen Shot 2015-11-16 at 11.36.45 AM.png <span class="size">9.07 KB</span></figcaption></figure></a>'.replace(/"/g, "'"))
&#13;