如何在Javascript中将字符串值从变量转换为HTML

How to convert string values from variable to HTML in Javascript

本文关键字:变量 转换 HTML 字符串 Javascript      更新时间:2023-09-26

如何在Javascript中将字符串值更改为HTML

问题

我的表单将HTML代码呈现为字符串,我没有控件。

它是动态渲染的。

现在我需要更改字符串内容以呈现为HTML

我在var中有以下值,检查作为字符串

var checking = formEl.innerHTML;

var检查中的字符串值是

 <thead id="updateContent">
  <tr>
    <th style="width:25%" class="spacingInHeader">Years of Expierence </th>
    <th style="width:50%" class="spacingInHeader">Salary</th>
    <th style="width:25%" class="spacingInHeader">Actions</th>
    <input type="hidden" id="editRowIndex" value="">    
  </tr>
</thead>
<tbody>
  <tr id="divUserList">
    <td class="dynamicRow"> <input style="width:100px!important;" type="text" id="totalExperience"> </td>
    <td class="dynamicRow"> <input type="text" id="salaryForThisPosition"> </td>
  </tr>
  <tr id="alf-id29">
    <td style="border-top-width: 1px; border-top-style: solid; border-top-color: rgb(197, 197, 197);">1</td>
    <td style="border-top-width: 1px; border-top-style: solid; border-top-color: rgb(197, 197, 197);">1</td>
    <td style="border-top-width: 1px; border-top-style: solid; border-top-color: rgb(197, 197, 197);"><a class="addLink" style="margin-right:10px" href="#" onclick="javascript:editRow(this);return false;"> Edit </a> <a href="#" class="addLink" onclick="javascript:deleteRow(this);return false;"> Delete </a> </td>
  </tr>
</tbody>

我想将这些字符串值呈现为HTML

更多解释

我在检查变量中有值

在该字符串值中,我有一个idupdateContent,我想将这些字符串值呈现为HTML

我有什么办法可以做到这一点吗。

提前谢谢。

将您想要更改的html放入另一个div中。并更改该div的内部html

例如,如果我们有一个非常简单的代码:

<p>Hello~!</p>

你想用html来自由修改它说。。。

<h1>Goodbye?!</h1>

你做的很简单:你把它放在一个类似这样的div中:

<div id="myName"><p>Hello~!</p></div>

您可以在javascript:中执行此操作

document.getElementById("myName").innerHTML = "<h1>Goodbye?!</h1>"

那。。。真的!

Js为那些想要检查的人出价:https://jsfiddle.net/9az241sf/