javascript 为什么innerHtml 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15120572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why innerHtml doesn't work
提问by mattytommo
Why doesn't innerHtml
work?
为什么不起作用innerHtml
?
<script src="jquery.js"></script>
<script type="text/javascript">
function chat()
{
alert ("started!");
document.getElementById("test").innerHtml="foo";
}
</script>
<body id="body" onload="chat();">
<p id="test">
test..
</p>
</body>
"started!" is showing, but the content of <p>
doesn't get changed to foo
as intended. Why?
“开始了!” 正在显示,但内容<p>
未按foo
预期更改。为什么?
回答by mattytommo
Casing matters, it's innerHTML
notinnerHtml
. Try this:
外壳很重要,它innerHTML
不是innerHtml
。试试这个:
document.getElementById("test").innerHTML="foo";
回答by Tom McDonald
If you're dealing with an API that returns XML then you likely don't have any innerHtml. Most browsers support outerHtml. Try this
如果您正在处理返回 XML 的 API,那么您可能没有任何 innerHtml。大多数浏览器都支持outerHtml。试试这个
function getHTML(node){
if(!node || !node.tagName) return '';
if(node.outerHTML) return node.outerHTML;
// polyfill:
var wrapper = document.createElement('div');
wrapper.appendChild(node.cloneNode(true));
return wrapper.innerHTML;
}
回答by impiyush
JavaScript is a Case Sensitive language so you should always check the case of the code you are writing.
JavaScript 是一种区分大小写的语言,因此您应该始终检查您正在编写的代码的大小写。
Try using "innerHTML" in place of "innerHtml".
尝试使用“innerHTML”代替“innerHtml”。
Also, it is a good practice to use Single Quotes(') instead of Double Quotes(") in JavaScript.
此外,在 JavaScript 中使用 Single Quotes(') 而不是 Double Quotes(") 也是一种很好的做法。
回答by Adam Arvan
If you are using a mac try using
如果您使用的是 mac 尝试使用
document.getElementById("test").firstChild.nodeValue = "foo";
document.getElementById("test").firstChild.nodeValue = "foo";