admin管理员组文章数量:1316980
I have a json object which contains some HTML. For example:
{
"cat": "1",
"catUrl": "this-is-a-url",
"catSummary": "This is a summary with <a href=\"\">a link</a>"
},
Notice catSummary has an href in it. But it doesn't get rendered as a link. Instead it just renders as regular text..
How do I make this work as a proper link?
EDIT
Just to clarify, I am using the VueJS framework, not jQuery.
A simple solution (that I pletely missed..) is using the v-html directive.
I have a json object which contains some HTML. For example:
{
"cat": "1",
"catUrl": "this-is-a-url",
"catSummary": "This is a summary with <a href=\"http://www.a.\">a link</a>"
},
Notice catSummary has an href in it. But it doesn't get rendered as a link. Instead it just renders as regular text..
How do I make this work as a proper link?
EDIT
Just to clarify, I am using the VueJS framework, not jQuery.
A simple solution (that I pletely missed..) is using the v-html directive.
- 1 Create a working snippet that demonstrate the issue you are facing. – gurvinder372 Commented Mar 22, 2018 at 7:14
-
2
element.innerHTML = "This is a summary with <a href=\"http://www.a.\">a link</a>"
– Slai Commented Mar 22, 2018 at 7:19
4 Answers
Reset to default 3In case all you want to do is populate another html element with the content in the catSummary field, use following:
$('#element_id').innerHTML = object_name.catSummary;
This will populate the element's object with the content in catSummary field.
Do you use any library ? For example with jQuery you could simply do like this:
var data = {
"cat": "1",
"catUrl": "this-is-a-url",
"catSummary": "This is a summary with <a href=\"http://www.a.\">a link</a>"
};
$("body").html(data.catSummary);
and it will render as a link.
You can use below method after getting "catSummary" value from your json
$.parseHTML("This is a summary with <a href=\"http://www.a.\">a link</a>")
Above method load your string as HTML and you can load into any element, See Reference
You can get your json property "catSummary" and set into HTML controls
$("#" + elementId).innerHTML = "This is a summary with <a href=\"http://www.a.\">a link</a>";
本文标签: javascriptConvert JSON string to HTMLStack Overflow
版权声明:本文标题:javascript - Convert JSON string to HTML - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742009870a2412689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论