变量分配给 getElementById,不起作用

发布于 2024-12-03 11:49:35 字数 738 浏览 3 评论 0原文

我尝试在 stackoverflow 上查找,但找不到与我相同的问题 - 我确信这个问题很容易解决,但不知何故......它不起作用。

我有一些输入,将以计算器格式进行加/乘/减/除等。 我想要的是通过在功能块之外声明变量来删除对 getElementById 不必要的引用(我不想要任何花哨的多个 getElementById 分配)。

我的 Javascript 如下:

//Declarations: designed to minimze calls to document.getElementByID
    var number1 = document.getElementById("num1");
    var number2 = document.getElementById("num2");
    var numAnswer = document.getElementById("answer");

    //Add together two numbers
    function add()
    {
        numAnswer.value = parseFloat(number1.value) + parseFloat(number2.value);
    }

它让我发疯 - 如果我取出变量并只使用普通的旧 document.getElementById,一切都会正常。这应该很容易,但它就是行不通。我检查了拼写,一切看起来都不错 - 是我遗漏了什么吗?

I've tried looking on stackoverflow, but can't quite find the same issue as me - I'm certain the issue is easy to solve, but somehow...it's not working.

I have some inputs, which will add/multiply/subtract/divide etc, in a calculator format.
What I want, is to remove unnecessary references to getElementById by declaring the variables outside of my function block (I don't want any fancy multiple-getElementById assignments).

My Javascript is below:

//Declarations: designed to minimze calls to document.getElementByID
    var number1 = document.getElementById("num1");
    var number2 = document.getElementById("num2");
    var numAnswer = document.getElementById("answer");

    //Add together two numbers
    function add()
    {
        numAnswer.value = parseFloat(number1.value) + parseFloat(number2.value);
    }

It's driving me insane - if I take out the variables and just use plain old document.getElementById, everything works. This should be so easy, but it's just not working. I've checked spelling, and it all seems okay - is there something I'm missing?

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

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

发布评论

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

评论(2

゛清羽墨安 2024-12-10 11:49:35

只要您在问题中显示的脚本出现在页面源中的元素之后,此操作就会起作用。否则浏览器尚未解析这些元素,因此无法通过 ID 找到它们。

让它工作的另一种方法是在 onload 或 document-ready 处理程序中进行分配,因为到 onload 或 document-ready 发生时所有 元素无论位于页面源代码中的哪个位置都可以访问。

This will work provided the script you show in your question appears after the elements in your page source. Otherwise the browser hasn't parsed those elements yet so it can't find them by ID.

The other way to get it to work is to do the assignments in an onload or document-ready handler, because by the time onload or document-ready occurs all elements are accessible no matter where they are in the page source.

一瞬间的火花 2024-12-10 11:49:35

您的代码工作得很好,因为您已将其包含在问题中。您可以在这里看到:http://jsfiddle.net/jfriend00/kXrDF/

这意味着您的问题中还存在其他错误。也许您可以包含更多代码,以便我们可以帮助您找到可能导致该问题的其他原因。

要测试“全局”变量的范围,您可以在 add() 函数中设置断点并检查 number1、number2 和 numAnswer 的值。如果您还没有弄清楚如何设置断点和检查变量,我强烈建议您这样做。如果仍然不能,那么您可以将临时测试放入 add() 代码中,如下所示,以缩小问题范围:

if (!number1) alert("number1 is not valid");
if (!number2) alert("number2 is not valid");
if (!numAnswer) alert("numAnswer is not valid");

另一种可能性是您的代码在页面加载之前运行得太早,在这种情况下初始代码无法找到 DOM 元素,因为它们还不存在。为了防止出现问题,您需要确保代码在页面加载后才运行。您可以通过将代码放置在页面元素之后或使用多种技术之一来检测页面何时加载,并且在此之前不运行初始化全局变量的代码来实现此目的。 jQuery 和 YUI 等框架都具有内置函数,可帮助您在页面加载后运行代码。

Your code works perfectly fine as you've included it in your question. You can see that here: http://jsfiddle.net/jfriend00/kXrDF/.

What that means is that there is something else wrong that you have not included in your question. Perhaps you can include more of your code so we can help you find what else might be causing it.

To test the scope of your "global" variables, you can set a break point in the add() function and check the values of number1, number2 and numAnswer. If you haven't yet figured out how to set a breakpoint and inspect variables, I'd highly recommend doing so. If you still can't, then you can put temporary tests into your add() code like this to narrow down what the issue is:

if (!number1) alert("number1 is not valid");
if (!number2) alert("number2 is not valid");
if (!numAnswer) alert("numAnswer is not valid");

The other possibility is that your code is running too early before the page has been loaded in which case the initial code can't find the DOM elements because they don't exist yet. To prevent that from being an issue, you need to make sure your code doesn't run until after the page has loaded. You can do that by placing your code after the page elements or by using one of the various techniques for detecting when the page has loaded and not running your code that initialized your global variables until then. The frameworks like jQuery and YUI all have built in functions for helping you run code after the page has been loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文