简体   繁体   English

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

[英]What is the correct way to refer to HTML elements via JavaScript?

I am making a colour-picker using pure JavaScript and HTML. 我正在使用纯JavaScript和HTML进行颜色选择。 It consists of three HTML selects (drop downs boxes) and one div the background-colour of which will be changed by JavaScript. 它由三个HTML选择(下拉框)和一个div组成,背景色将由JavaScript更改。 I am also trying to do this as "correctly" as possible. 我也在尝试尽可能“正确”地执行此操作。 This means no Javascript code in the HTML. 这意味着HTML中没有Javascript代码。

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: 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>

The trouble is that all the document.getElementById() calls return null. 问题在于所有document.getElementById()调用都返回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. 我曾尝试将代码放在window.onload = function(){}中,但是a)只会引起混淆,b)然后我必须在一个函数中定义update函数,这似乎不太正确。

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>

putting code in a handler for the onload event works just fine, and is widely accepted. 将代码放入onload事件的处理程序中可以很好地工作,并且被广泛接受。 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 这是教务长爱德华兹(dean edwards)撰写的有关使用window.onload的博客文章

Another option is to put your javascript at the bottom of the page instead of at the top. 另一个选择是将您的JavaScript放在页面底部,而不是顶部。

As the others said you will need to wrap your code inside a function and call it from the window.onload event.. 正如其他人所说,您将需要将代码包装在一个函数中,然后从window.onload事件中调用它。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引用 HTML/JavaScript/CSS 的另一种方法是什么? - what is an alternate way to refer to HTML/JavaScript/CSS? 使用 Javascript 编写 HTML 的正确方法是什么? - What is the correct way to write HTML using Javascript? 在页面加载之前隐藏HTML元素的正确方法是什么? - What is the correct way to hide HTML elements on page load before they are displayed? 通过javascript为HTML元素赋值的方式有什么区别? - what is the difference between ways of assigning values to HTML elements via javascript? 使用 CSS 和 Javascript 向 HTML DOM 对象添加动画的正确方法是什么? - What is the correct way of adding animation to an HTML DOM object with CSS and Javascript? 在构建html时,在javascript中支持撇号的正确方法是什么? - What is the correct way to support apostrophes in javascript when building up html? HTML5 在锚点中使用 Javascript 的正确方法是什么? - What is the correct way in HTML5 to use Javascript in anchors? 从Javascript类中的静态方法引用成员函数的正确方法 - Correct way to refer to a member function from a static method in a Javascript class 在javascript中引用渐变背景颜色的正确语法是什么 - What is the correct syntax to refer to a gradient background color in javascript 通过JavaScript对HTML表进行排序的最有效方法是什么? - What is the most efficient way to sort an HTML table via JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM