当我将html
转换成json
友好的时候,我从json
得到了讨厌的双引号。
它可以在下面的代码中看到,在<p>"
的开头和"<\p>
的结尾。我怎样才能删除它们。尝试跟随不起作用。
$("p").html().replace(/['"]+/g, '')
这是js变量中的动态html。
<p>"<!--HTML icon appears here--><a href="https://www.example.com/viewer/event.jsp?ei=1434586&tp_key=e8ced8705c" target="_blank">Click here for web</a><br>
<!--PDF icon appears here--><a href="../file/408427349/Index?KeyFile=1500113055" target="_blank">Press Release</a><br>
<!--PDF icon appears here--><a href="../file/4234/Index?KeyFile=234324" target="_blank">somelink</a><br>"</p>
发布于 2018-10-05 13:00:26
你是不是忘了在清理文本后设置html?
这应该可以做到:
var text = $('p').html().replace(/['"]+/g, '')
$('p').html(text)
// or you can just:
// $('p').html($('p').html().replace(/['"]+/g, ''))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>"<!--HTML icon appears here--><a href="https://event.web.com/viewer/event.jsp?ei=1434586&tp_key=e8ced8705c" target="_blank">Click here for web</a><br>
<!--PDF icon appears here--><a href="../file/408427349/Index?KeyFile=1500113055" target="_blank">Press Release</a><br>
<!--PDF icon appears here--><a href="../file/4234/Index?KeyFile=234324" target="_blank">somelink</a><br>"</p>
发布于 2018-10-05 13:01:52
可以使用Element.outerHTML属性删除任何'
或"
引号。
/[><]['"]+[><]/g
将删除开始标记和结束标记之间的多余引号。它不会从attribute = value
对中删除引号。
let p = document.querySelector('p');
p.outerHTML = p.outerHTML.replace(/[><]['"]+[><]/g, '');
<p>"<!--HTML icon appears here--><a href="https://event.web.com/viewer/event.jsp?ei=1434586&tp_key=e8ced8705c" target="_blank">Click here for web</a><br>
<!--PDF icon appears here--><a href="../file/408427349/Index?KeyFile=1500113055" target="_blank">Press Release</a><br>
<!--PDF icon appears here--><a href="../file/4234/Index?KeyFile=234324" target="_blank">somelink</a><br>"</p>
https://stackoverflow.com/questions/52658536
复制相似问题