Javascript 类型错误:document.getElementbyId 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14171212/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:54:40  来源:igfitidea点击:

TypeError: document.getElementbyId is not a function

javascripthtml

提问by Zeta

In the following snippet everything works as expected, but when I click "Show Source" Firefox produces this error:

在以下代码段中,一切都按预期工作,但是当我单击“显示源”时,Firefox 会产生此错误:

--
[11:07:30.630] TypeError: document.getElementbyId is not a function @ http://localhost:8888/html5/native-rich-text.html:10

And Safari produces a similar error. What is causing this?

Safari 也会产生类似的错误。这是什么原因造成的?

function showSource() {
  var content = document.getElementbyId("edit").innerHTML
  content.replace(/</g, '&lt;');
  content.replace(/>/, '&gt: ');
  prompt("Your Code:", content);

}

function createLink() {
  var url = prompt("Enter URL:", "http://");
  if (url)
    document.execCommand("createlink", false, url);
}
<h1>Native Rich Text</h1>
<p>No textboxes here, that's a &lt;div&gt; element!</p>
<div>
  <input type="button" value="Bold" onclick="document.execCommand('bold', false, null);">
  <input type="button" value="Italic" onclick="document.execCommand('italic', false, null);">
  <input type="button" value="Underline" onclick="document.execCommand('underline', false, null);">
  <input type="button" value="Add Link" onclick="createLink();">
  <input type="button" value="Show Source" onclick="showSource();">
</div>
<div id="edit" style="border:solid black; height: 300px; width: 400px;" contenteditable="true">
  Hello!
</div>

回答by Zeta

Case sensitive: document.getElementById(notice the capital B).

区分大小写:(document.getElementById注意大写B)。

回答by Mattias Buelens

JavaScript is case-sensitive. The bin getElementbyIdshould be capitalized.

JavaScript 区分大小写。该bgetElementbyId应大写。

var content = document.getElementById("edit").innerHTML;