use*_*266 -2 javascript string dom
我试图改变<div id="demo"></div>到<div id="demo">Hello<World.</div>
在我的例子中问题是结果<div id="demo">Hello</div>.
我怎样才能得到结果<div>Hello<world.</div>?
例:
var str = "Hello<world.";
document.getElementById("demo").innerHTML = str; // HelloRun Code Online (Sandbox Code Playgroud)
<div id="demo"></div>Run Code Online (Sandbox Code Playgroud)
ASD
你正在使用innerHTML它需要一个HTML字符串.由于<用于在HTML中创建标记,因此该单词world被解释为标记名称.
textContent改为使用:
var str = "Hello<world.";
document.getElementById("demo").textContent = str;Run Code Online (Sandbox Code Playgroud)
<div id="demo"></div>Run Code Online (Sandbox Code Playgroud)