document.getElementById innerHTML没有显示 [英] document.getElementById innerHTML not displaying
问题描述
这应该是一件非常容易的事,但它不会返回任何东西。
函数love()应该启动,获得一个简单的数字提示,然后吐出一些使用该起始编号的项目列表。
警告框正确显示我的预期,但我希望它显示在屏幕上。
(这只是我所追求的一小部分,但它是它的核心)。 IE,FF或Chrome中没有显示任何文字......
This should be a pretty easy thing to do, but it's not returning anything. The function love() should kick off, getting a simple number prompt, and spitting out a list of a few items that uses that starting number. the alert box correctly displays what I expect, but I want it to display on the screen. (this is but a small section of what I'm after, but it's the kernel of it). No text is displaying in the IE, FF, or Chrome...
<script type="text/javascript">
function love()
{
var ncxElement="";
var idNumber = prompt("Enter beginning number","");
var myText=document.getElementById("here");
for (var i=1;i<5;i++)
{
ncxElement+=("<navPoint class=\"other\" id=\"page_"+idNumber+"\">\n");
idNumber++;
}
alert(ncxElement);
myText.innerHTML=ncxElement;
}
</script>
</head>
<body onload="love()">
<p id="here">Begin!</p>
</body>
推荐答案
如果你想在你的页面上显示HTML(没有它被解析),使用 .textContent
而不是 .innerHTML
并将其包装在< ; pre>
(以保留换行符)。
If you want to display HTML on your page (without it being parsed), use .textContent
instead of .innerHTML
and wrap it in a <pre>
(to preserve the line breaks).
演示:
更改:
myText.innerHTML=ncxElement;
收件人:
myText.textContent=ncxElement;
更改:
<p id="here">Begin!</p>
收件人:
<pre id="here">Begin!</pre>
这篇关于document.getElementById innerHTML没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!