繁体   English   中英

将HTML标记设置为对象的值

[英]Set HTML tag as a value of an object

我有一个HTML标记,我想在对象中显示为HTML(不是字符串),问题是当我显示它时,它仍然是文本而不是HTML

var obj = {  "CategoryItemID": "ID",
             "ItemName": "name",
             "options":"<a title='Edit' ng-click='edit()' id='btnEdit'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></a> |" +
                        "<a title='Delete' ng-click='delete()' " +
                        "id='btnDelete'><span style='color:red' class='fa fa-times' aria-hidden='true'></span></a>",
                           "children": "",
          }

如何从对象读取为HTML?

使用DOM分析器您可以在此处作为示例

  var obj = { "CategoryItemID": "ID", "ItemName": "name", "options":"<a title='Edit' ng-click='edit()' id='btnEdit'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></a> |" + "<a title='Delete' ng-click='delete()' " + "id='btnDelete'><span style='color:red' class='fa fa-times' aria-hidden='true'></span></a>", "children": "", } var parser = new DOMParser() var node = parser.parseFromString(obj.options, "text/html"); console.log(node.body.innerHTML) 

想要访问的内容都可以如下访问

node.body.getElementsByTagName("a")
node.body.getElementsByClassName("something")

试试这个在表中显示

  var obj = { "CategoryItemID": "ID", "ItemName": "name", "options":"<a title='Edit' ng-click='edit()' id='btnEdit'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></a> |" + "<a title='Delete' ng-click='delete()' " + "id='btnDelete'><span style='color:red' class='fa fa-times' aria-hidden='true'></span></a>", "children": "", } var parser = new DOMParser() var node = parser.parseFromString(obj.options, "text/html"); document.getElementsByClassName("option")[0].innerHTML=node.body.innerHTML; 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.0/css/all.min.css"/> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"/> <table> <tr> <th>Name</th> <th>Option</th> </tr> <tr> <td>Sourabh</td> <td class="option"></td> </tr> </table> 

创建一个虚拟DOM元素,然后向其中添加HTML字符串。 然后,您可以像处理任何DOM元素一样操作它。

var el = document.createElement( 'html' );
el.innerHTML = obj.options;
const stringContainingHTMLSource = obj.options;
const parser = new DOMParser();
const doc = parser.parseFromString(stringContainingHTMLSource, "text/html");

解析SVG或HTML文档

将对象属性分配给要显示的相应元素的innerHTML。

HTML

<div id="displayObject">
</div>

JS

var displayObj = document.getElementById('displayObject');
displayObj.innerHTML = obj.options;

是否可以设置 a 的 value 属性<textarea><i>tag to be the source code of an html file?</div></i><b>标记为 html 文件的源代码?</div></b></textarea><div id="text_translate"><p> 我想知道是否可以将 <textarea> 标记的值属性设置为网页的 HTML。</p><p> 我试图将值设置为等于本地文件“./file.html”的 HTML。</p><pre> document.getElementById("textareaTag").value =;</pre><p> 由于我是 JavaScript 的新手,所以我没有解决方案,尽管可能有一个。 谁能帮我?</p><p> 注意:一些挖掘使得它看起来可能使用 window.open() 命令来完成此操作。 行得通吗? 如果是这样,如何?</p></div>

[英]Is it possible to set the value property of a <textarea> tag to be the source code of an html file?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM