被 javascript 困住了(初学者)

问题描述 投票:0回答:1

我刚刚开始学习 javascript,并且我被困在我的第一个实验室中。我的 HTML 部分可以工作,但 javascript 却没有工作。起初我认为它没有正确地将 javascript 代码链接到 HTML 代码,但现在我认为 onload 和 oninput 部分存在问题。但不知道为什么。如果有人可以提供帮助,将不胜感激。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Lab 11 suits</title>
    <script src="Lab1.js"></script>
</head>
<body>
<heading>
    <h1>
        HTML 5 Test Page
    </h1>
</heading>
<p id="test"></p>
<button id="button">Press Me</button>
<p/>
Colours:<select id="list">
    <option>Red</option>
    <option>Green</option>
    <option>Blue</option>
</select>
<p/>
Your Birthday:<input type="date" id="dod"/>
A Number: <input type="range" id="range" min="1" max="10" value="1">
<span id="value">1</span>
</body>
</html>
window.onload = function() {
    var para = document.getElementById("heading");
    para.innerText = "A short exercise on creating dynamic web content.";
    var button = document.getElementById("button");
    button.onclick = function () {
        alert("Ive been clicked");
    };

    var list = document.getElementById("list");
    list.onchange = function () {
        var item = list.options[list.selectedIndex].text;
        changeColour(item);
    };
    var dob = document.getElementById("dob");
    dob.oninput = function () {
        alert("Your birth date is:" + dob.value);
    };
    var range = document.getElementById("range");
    var value = document.getElementById("value");
    range.onchange = function () {
        value.innerText = range.value;
    };

    function changeColour(colour) {
        var c = 0;
        switch (colour) {
            case "Red":
                c = "#f00";
                break;
            case "Green":
                c = "#0f0";
                break;
            case "Blue":
                c = "#00f";
                break;
        }
        document.bgColor = c;
    };

    function daysOld(dob) {
        var msPerDay = 1000 * 60 * 60 * 24,
            now = new Date(),
            diff = now - dob;
        return diff / msPerDay;
    };

};
javascript html dom
1个回答
0
投票

我做了一些编辑:

  • heading
    元素上缺少 id
  • 您的
    id="dob"
    有错字
    id="dod"

window.onload = function() {

    /******* EDIT 1 ********/
    // the heading element has no ID, so you need to add one, or use `querySelector` instead.
    //var para = document.getElementById("heading");
    
    var para = document.querySelector("heading");
    /***********************/
    
    para.innerText = "A short exercise on creating dynamic web content.";
    var button = document.getElementById("button");
    button.onclick = function () {
        alert("Ive been clicked");
    };

    var list = document.getElementById("list");
    list.onchange = function () {
        var item = list.options[list.selectedIndex].text;
        changeColour(item);
    };
    var dob = document.getElementById("dob");
    dob.oninput = function () {
        alert("Your birth date is:" + dob.value);
    };
    var range = document.getElementById("range");
    var value = document.getElementById("value");
    range.onchange = function () {
        value.innerText = range.value;
    };

    function changeColour(colour) {
        var c = 0;
        switch (colour) {
            case "Red":
                c = "#f00";
                break;
            case "Green":
                c = "#0f0";
                break;
            case "Blue":
                c = "#00f";
                break;
        }
        document.bgColor = c;
    };

    function daysOld(dob) {
        var msPerDay = 1000 * 60 * 60 * 24,
            now = new Date(),
            diff = now - dob;
        return diff / msPerDay;
    };

};
   <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Lab 11 suits</title>
    <script src="Lab1.js"></script>
</head>
<body>
<heading>
    <h1>
        HTML 5 Test Page
    </h1>
</heading>
<p id="test"></p>
<button id="button">Press Me</button>
<p/>
Colours:<select id="list">
    <option>Red</option>
    <option>Green</option>
    <option>Blue</option>
</select>
<p/>
<!-- EDIT 2 -->
<!-- You had a typo on the id of your input 'dod' -> 'dob' -->
<!-- Your Birthday:<input type="date" id="dod"/> -->
Your Birthday:<input type="date" id="dob"/>

<!-- End EDIT -->
A Number: <input type="range" id="range" min="1" max="10" value="1">
<span id="value">1</span>
</body>
</html>

© www.soinside.com 2019 - 2024. All rights reserved.