我写了以下代码:
//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('"','\"');
ntext = ntext.replace('>','>');
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"><span id="newid" style="font-size: 10pt;">Write something! To Write,just press the edit button above...</span></div>