通过 JavaScript 引用 HTML 元素的正确方法是什么?

发布于 2024-08-18 04:41:36 字数 2332 浏览 5 评论 0原文

我正在使用纯 JavaScript 和 HTML 制作一个颜色选择器。它由三个 HTML 选择(下拉框)和一个 div 组成,其背景颜色将由 JavaScript 更改。我也在尝试尽可能“正确”地做到这一点。这意味着 HTML 中没有 Javascript 代码。

到目前为止,我的代码如下所示:

    var red = document.getElementById('red');
    red.onchange = update();

    var green = document.getElementById('green');
    green.onchange = update();

    var blue = document.getElementById('blue');
    blue.onchange = update();

    var thebox = document.getElementById('colourbox');

    function d2h(d) {return d.toString(16);}
    function h2d(h) {return parseInt(h,16);} 

    function update(){
        finalcolor = '#' + d2h(red.value) + d2h(green.value) + d2h(blue.value)
        thebox.style.background = finalcolour;
    }

HTML 如下所示:

<div id="colourbox"></div>
<form name="myform" action="colour.html">
    <select name="red" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
    <select name="green" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
    <select name="blue" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
</form>

问题是所有 document.getElementById() 调用都返回 null。大概是因为它们在代码运行时不存在。我尝试将代码放入 window.onload = function() {} 中,但是 a) 这会让人感到困惑,b) 然后我必须在函数中定义更新函数,这似乎不对。

有人能解释一下吗?是否有任何一般规则可以帮助我理解它是如何工作的?或者有关该主题的一些文档?

编辑:修改后的代码:

<script type="text/javascript">
    window.onload = function(){
        var red = document.getElementById('red');
        red.onchange = update();

        var green = document.getElementById('green');
        green.onchange = update();

        var blue = document.getElementById('blue');
        blue.onchange = update();

        var thebox = document.getElementById('colourbox');

        function d2h(d) {return d.toString(16);}
        function h2d(h) {return parseInt(h,16);} 

        function update(){
            var finalcolor = '#' + d2h(document.getElementById('red').value) + d2h(document.getElementById('green').value) + d2h(document.getElementById('blue').value);

            document.getElementById('colourbox').style.background = finalcolor;
        }

    }
</script>

I am making a colour-picker using pure JavaScript and HTML. It consists of three HTML selects (drop downs boxes) and one div the background-colour of which will be changed by JavaScript. I am also trying to do this as "correctly" as possible. This means no Javascript code in the HTML.

My code so far looks like this:

    var red = document.getElementById('red');
    red.onchange = update();

    var green = document.getElementById('green');
    green.onchange = update();

    var blue = document.getElementById('blue');
    blue.onchange = update();

    var thebox = document.getElementById('colourbox');

    function d2h(d) {return d.toString(16);}
    function h2d(h) {return parseInt(h,16);} 

    function update(){
        finalcolor = '#' + d2h(red.value) + d2h(green.value) + d2h(blue.value)
        thebox.style.background = finalcolour;
    }

And the HTML looks like this:

<div id="colourbox"></div>
<form name="myform" action="colour.html">
    <select name="red" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
    <select name="green" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
    <select name="blue" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
</form>

The trouble is that all the document.getElementById() calls return null. Presumably because they don't exist at the time that the code is run. I have tried putting the code inside an window.onload = function() {} but a) that just gets confusing and b) I would then have to define the update function inside a function, which doesn't seem right.

Can anyone shed some light on this? Are there any general rules which might help me understand how it works? Or some documentation on the subject?

EDIT: revised code:

<script type="text/javascript">
    window.onload = function(){
        var red = document.getElementById('red');
        red.onchange = update();

        var green = document.getElementById('green');
        green.onchange = update();

        var blue = document.getElementById('blue');
        blue.onchange = update();

        var thebox = document.getElementById('colourbox');

        function d2h(d) {return d.toString(16);}
        function h2d(h) {return parseInt(h,16);} 

        function update(){
            var finalcolor = '#' + d2h(document.getElementById('red').value) + d2h(document.getElementById('green').value) + d2h(document.getElementById('blue').value);

            document.getElementById('colourbox').style.background = finalcolor;
        }

    }
</script>

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

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

发布评论

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

评论(2

木緿 2024-08-25 04:41:36

将代码放入 onload 事件的处理程序中效果很好,并且被广泛接受。将函数放入函数中也是如此(取决于原因)。

var mypage = {
    red: null,
    green: null,
    blue: null,
    colourbox: null,

    d2h: function(d) {
        var hex = d.toString(16);
        if (hex.length < 2) {
            hex = '0' + hex;
        }
        return hex.toUpperCase();
    },

    h2d: function(d) {
        return parseInt(h, 16);
    },

    update: function() {
        mypage.colourbox.style.backgroundColor = '#' + mypage.d2h(mypage.red.value) + mypage.d2h(mypage.green.value) + mypage.d2h(mypage.blue.value);
    }
};

window.onload = function() {
    mypage.red = document.getElementById('red');
    mypage.red.onchange = mypage.update;

    mypage.green = document.getElementById('green');
    mypage.green.onchange = mypage.update;

    mypage.blue = document.getElementById('blue');
    mypage.blue.onchange = mypage.update;

    mypage.colourbox = document.getElementById('colourbox');
}

这是 dean edwards 关于使用 window.onload 的博客文章

另一种选择是将 JavaScript 放在页面底部而不是顶部。

putting code in a handler for the onload event works just fine, and is widely accepted. So is putting a function within a function (depending on the reason).

var mypage = {
    red: null,
    green: null,
    blue: null,
    colourbox: null,

    d2h: function(d) {
        var hex = d.toString(16);
        if (hex.length < 2) {
            hex = '0' + hex;
        }
        return hex.toUpperCase();
    },

    h2d: function(d) {
        return parseInt(h, 16);
    },

    update: function() {
        mypage.colourbox.style.backgroundColor = '#' + mypage.d2h(mypage.red.value) + mypage.d2h(mypage.green.value) + mypage.d2h(mypage.blue.value);
    }
};

window.onload = function() {
    mypage.red = document.getElementById('red');
    mypage.red.onchange = mypage.update;

    mypage.green = document.getElementById('green');
    mypage.green.onchange = mypage.update;

    mypage.blue = document.getElementById('blue');
    mypage.blue.onchange = mypage.update;

    mypage.colourbox = document.getElementById('colourbox');
}

Here is a blog post by dean edwards about using window.onload

Another option is to put your javascript at the bottom of the page instead of at the top.

奈何桥上唱咆哮 2024-08-25 04:41:36

正如其他人所说,您需要将代码包装在函数中并从 window.onload 事件调用它。

此外当您将函数分配给事件时,仅使用名称(无括号)或者该函数就在那里被调用,然后..

所以

red.onchange = update();

应该是

red.onchange = update;

As the others said you will need to wrap your code inside a function and call it from the window.onload event..

Also when you assign a function to an event, use the name only (no parenthesis) or the function gets called right there and then ..

so

red.onchange = update();

should be

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