document.getElementById('id') is null JavaScript 错误

发布于 2024-09-27 05:13:28 字数 2112 浏览 5 评论 0原文

由于某种原因,我在第 7 行收到 document.getElementById('id') is null JS 错误,其中包含以下标记和脚本:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
document.getElementById('calculate').onclick = function calculateQuad()
{
    var inputa = document.getElementById('variablea').value;
    var inputb = document.getElementById('variableb').value;
    var inputc = document.getElementById('variablec').value;

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = (-inputb + Math.sqrt(root))/2*inputa
    root2 = (-inputb + Math.sqrt(root))/2*inputa 

    document.getElementById('root1').value = root1;
    document.getElementById('root2').value = root2;
    if(root<'0')
    {
        alert('This equation has no real solution.')
    }
    else {
        if(root=='0')
        {
            document.getElementById('root1').value = root1
            document.getElementById('root2').value = 'No Second Answer'
        }
        else {
            document.getElementById('root1').value = root1
            document.getElementById('root2').value = root1
            }
        }
};
document.getElementById('erase').onlick = this.form.reset();
</script>
<style>
#container
{
    text-align: center;
}
</style>
</head>

<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
    a:<input id="variablea" value="" type="text">
    <br/>
    b:<input id="variableb" value="" type="text">
    <br />
    c:<input id="variablec" value="" type="text">
    <br />
    <input id="calculate" value="Calculate!" type="button">
    <input id="erase" value="Clear" type="button">
    <br />
    <br />
    Roots:
    <br />
    <input id="root1" type="text" readonly>
    <br />
    <input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>

这里真正的问题是什么?

For some reason I get an document.getElementById('id') is null JS error on line 7 with the following markup and script:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
document.getElementById('calculate').onclick = function calculateQuad()
{
    var inputa = document.getElementById('variablea').value;
    var inputb = document.getElementById('variableb').value;
    var inputc = document.getElementById('variablec').value;

    root = Math.pow(inputb,2) - 4 * inputa * inputc;
    root1 = (-inputb + Math.sqrt(root))/2*inputa
    root2 = (-inputb + Math.sqrt(root))/2*inputa 

    document.getElementById('root1').value = root1;
    document.getElementById('root2').value = root2;
    if(root<'0')
    {
        alert('This equation has no real solution.')
    }
    else {
        if(root=='0')
        {
            document.getElementById('root1').value = root1
            document.getElementById('root2').value = 'No Second Answer'
        }
        else {
            document.getElementById('root1').value = root1
            document.getElementById('root2').value = root1
            }
        }
};
document.getElementById('erase').onlick = this.form.reset();
</script>
<style>
#container
{
    text-align: center;
}
</style>
</head>

<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
    a:<input id="variablea" value="" type="text">
    <br/>
    b:<input id="variableb" value="" type="text">
    <br />
    c:<input id="variablec" value="" type="text">
    <br />
    <input id="calculate" value="Calculate!" type="button">
    <input id="erase" value="Clear" type="button">
    <br />
    <br />
    Roots:
    <br />
    <input id="root1" type="text" readonly>
    <br />
    <input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>

What's the real problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赠意 2024-10-04 05:13:28

它确实是null。您的元素尚不存在于页面上。

或者:

  • 添加 window.onload 事件。

It is indeed null. Your element doesn't exist on the page yet.

Either:

  • Move your <script> block below your code
  • Add a window.onload event.
似狗非友 2024-10-04 05:13:28

或者:window.onload = function(){} 也可以

Alternatively: window.onload = function(){} works as well

等风来 2024-10-04 05:13:28

如果您不想包含 jQuery,那么 document.ready = function () {} 也可以正常工作。

If you don't feel like including jQuery, then document.ready = function () {} will work fine too.

榕城若虚 2024-10-04 05:13:28

在 javascript 实际运行时,不存在 id 为“calculate”的元素。您需要确保您的 javascript 在创建所需的元素后运行。我的建议是使用通用的 javascript 框架,如 jquery。然后,您只需附加脚本即可在 dom 上运行:

$(document).ready(function() {
    // Handler for .ready() called.
});

At the point which the javascript is actually run, there is no element with an id of 'calculate'. You need to make sure your javascript runs after your required elements have been created. My advice would be to use a common javascript framework like jquery. You would then just attach your script to run on dom ready:

$(document).ready(function() {
    // Handler for .ready() called.
});
夏尔 2024-10-04 05:13:28

您可以通过将脚本移至正文末尾来修复此问题,或者尝试使用 window.onloadwindow.addEventListenner
检查 http://v3.thewatchmakerproject.com/journal /168/javascript-the-dom-addeventlistener-and-attachevent
http://javascript.about.com/library/blonload.htm

You can fix it by moving the script to the end of the body, or try to use window.onload, or window.addEventListenner
Check the http://v3.thewatchmakerproject.com/journal/168/javascript-the-dom-addeventlistener-and-attachevent
and http://javascript.about.com/library/blonload.htm

雨轻弹 2024-10-04 05:13:28
  1. 使用 windows.onload
  2. 添加链接文件脚本或“正文结束标记”下方的脚本
  1. using windows.onload
  2. add link file script or script below "body end tag"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文