This question already has an answer here:
这个问题在这里已有答案:
- How to decode HTML entities using jQuery? 18 answers
如何使用jQuery解码HTML实体? 18个答案
I am using jquery to get the value of a hidden input. The hidden input contains a data-trix-attachment attribute with JSON object. The problem is that jquery replaced the quotes with "
. I am not sure why.
我使用jquery来获取隐藏输入的值。隐藏输入包含带有JSON对象的data-trix-attachment属性。问题是jquery用“"”替换了引号。我不知道为什么。
I have looked around for a solution, and can't seem to find one.
我四处寻找解决方案,似乎无法找到解决方案。
I tried doing a regex to replace the "
with ' but that didn't work either. (Below is an example)
我试着做一个正则表达式来取代“ '但这也没有用。 (下面是一个例子)
Can anyone please help me solve this. Below is the code I tried using.
任何人都可以帮我解决这个问题。以下是我尝试使用的代码。
var new1 = $("#trix-input-1").attr("value");
var e_encoded = new1.replace(/"/g, """);
HTML Code
<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 Response
<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 Code
var new1 = $("#trix-input-1").val();
1 个解决方案
#1
2
Wrong syntax for replace
, it's replace(oldStr, newStr)
not the other way round. See here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace
替换的语法错误,它替换(oldStr,newStr)而不是相反。请参见:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace
So this:
var e_encoded = new1.replace(/"/g, """);
should be:
var e_encoded = new1.replace(/"/g, "'");
You example works fine:
你的例子很好:
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, "'"))
With document.write instead of console output:
使用document.write而不是控制台输出:
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, "'"))
#1
2
Wrong syntax for replace
, it's replace(oldStr, newStr)
not the other way round. See here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace
替换的语法错误,它替换(oldStr,newStr)而不是相反。请参见:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace
So this:
var e_encoded = new1.replace(/"/g, """);
should be:
var e_encoded = new1.replace(/"/g, "'");
You example works fine:
你的例子很好:
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, "'"))
With document.write instead of console output:
使用document.write而不是控制台输出:
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, "'"))