javascript - GetElementById 在 Chrome 中给出 ReferenceError

标签 javascript css google-chrome getelementbyid

我不知道发生了什么,因为我从来没有遇到过 JS 问题,但我们开始吧。我不断在 Chrome 中使用以下代码收到 Uncaught ReferenceError:

function showShareButtons() {
    var buttons = getElementById("sharebtns");
    document.buttons.style.visibility = 'visible';
}

有人能告诉我这是怎么回事吗?谢谢!

最佳答案

您不能简单地为变量单独声明 getElementById。您需要使用 document.getElementById :

function showShareButtons() {
    var buttons = document.getElementById("sharebtns");
    buttons.style.visibility = 'visible';
}

即使您最终在代码中使用了 document,它也不会相同,因为无法将变量声明为任何确定的变量。

关于javascript - GetElementById 在 Chrome 中给出 ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596378/

相关文章:

javascript - chrome.storage.local.set() 无法正常工作

javascript - 为什么我的 three.js 场景中的聚光灯仍然以相机视角居中,但仅在 Chrome for Android 上?

Javascript reduce() 直到值之和 < 变量

javascript - React 有望在全局范围内可用

javascript - Angular ng-repeat 变化值

html - 将 li 左对齐 IE8

javascript - 将类添加到菜单 Jquery

javascript - 在滚动上添加和删除类

google-chrome - 如何在旧版 Chrome 上测试网站?

javascript - 最简单的 Jquery/Javascript 悬停窗口?