如何将字符串转换为div.innerHTML?

时间:2013-10-26 19:50:18

标签: javascript html

我写了以下代码:

//HTML Page

//onpage load 
<body onload="RenderContent('{{ name }}');"> 

  <div id="cWrite"></div> 
  <form method="POST" action="/save/">{% csrf_token %}
    <input type="submit" value="Save Page"/>
  </form>

Javascript功能:

function RenderContent(ctext)
{
  var ntext = ctext;

  ntext = ntext.replace('&alt;','<');
  ntext = ntext.replace('&quot;','\"');
  ntext = ntext.replace('&gt;','>');

  document.getElementById("cWrite").innerHTML = ntext;
}

发布内容后,name看起来像是:

<span id="newid" style="font-size: 10pt;">
  Write something! To Write,just press the edit button above...
</span> 
<span id="newid" style="font-size: 10pt;">
  Write something! To Write,just press the edit button above...
</span>

但我不希望它作为div innerHTML中的字符串,我希望它是HTML内容,RenderContent函数正常工作(它在警告上显示上述<span id...内容)< / p>

如何将其转换为div的innerHTML?

//Updated question

ok...dont call the function...what about this

 <div id="cWrite">{{ name }}</div> //html

和传递的字符串({{name}})是

<span id="newid" style="font-size: 10pt;">Write something! To Write,just press the edit button above...</span>

它显示字符串中的HTMLelements而不是HTML ....下面是POST后的HTML文件的源代码(我在{{name}}中得到的字符串)

<div id="cWrite">&lt;span id=&quot;newid&quot; style=&quot;font-size: 10pt;&quot;&gt;Write something! To Write,just press the edit button above...&lt;/span&gt;</div>

0 个答案:

没有答案