javascript - TypeError : document. getElementbyId 不是函数

标签 javascript html

<分区>

在下面的片段中,一切都按预期工作,但是当我点击“显示源代码”时,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?

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>

最佳答案

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

关于javascript - TypeError : document. getElementbyId 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14171212/

相关文章:

javascript - HTML 5 视频自动播放功能检测

javascript - 媒体元素 : how to get instance of the player

javascript - 仅提取特定单词后的 <string>/<string> 或 <string>

javascript - 用 javascript 制作基本的益智游戏

css - 外部 CSS 不在浏览器中呈现

javascript - 使用Javascript在mongo中数组indexOf

javascript - FireFox Javascript 事件处理

html - 在 div 中显示内联不同的元素

php - 搜索结果问题

c# - 使用 openXML 将 docx/doc 第一个页眉和页脚导出为 docx 文件