简体   繁体   English

如何创建一个按钮来清除信息?

[英]How to create a button to clear information?

I'm working on a JS project that creates 3 cars with 2 buttons underneath each car.我正在开发一个 JS 项目,该项目创建 3 辆汽车,每辆汽车下方有 2 个按钮。 One button shows the car information, the other is supposed to clear the information.一键显示汽车信息,一键清除信息。 I cannot seem to get the clear button to work.我似乎无法让清除按钮工作。 I've tried all kinds of things and I have a feeling it's something simple.我尝试了各种各样的事情,我觉得这很简单。 Please help, here's my code:请帮忙,这是我的代码:

<div id= "container">
<div class="row">
    <div class="column">
        <img src="images/Porsche.jpg">
        <button onclick="display1()" id="submit1">See Details</button>

        <button onclick="clear()" value="reset" id="clear">Clear Selection</button>
        
    </div>
    <div class="column">
        <img src="images/Ferrari.jpg">
        <button onclick="display2()" id="submit2">See Details</button>

        <button onclick="clear2()" id="clear">Clear Selection</button>
    </div>
    <div class="column">
        <img src="images/Lambo.jpg">
        <button onclick="display3()" id="submit3">See Details</button>
        
        <button onclick="clear3()" id="clear">Clear Selection</button>
    </div>
  </div>
</div>


var porsche = {
    make: "Porsche",
    model: "Panarama",
    year: 2020,
    available: true,
    description: "The Porsche Panamera is an excellent super luxury car."
}

function display1() {
    var place = document.getElementById("submit1");

    for (value in porsche) {
        place.innerHTML = "Make: " + porsche.make + "<br>" +
            "Model: " + porsche.model + "<br>" +
            "Year: " + porsche.year + "<br>" +
            "Available: " + porsche.available + "<br>" +
            "About: " + porsche.description;
    }
};

var ferrari = {
    make: "Ferrari",
    model: "Panarama",
    year: 2020,
    available: true,
    description: "Ranks near the top of super luxury cars, with a strong engine, well-balanced handling, and comfortable interior."
}

function display2() {
    var place = document.getElementById("submit2");

    for (value in ferrari) {
        place.innerHTML = "Make: " + ferrari.make + "<br>" +
            "Model: " + ferrari.model + "<br>" +
            "Year: " + ferrari.year + "<br>" +
            "Available: " + ferrari.available + "<br>" +
            "About: " + ferrari.description;
    }
};

var lambo = {
    make: "Lamborghini",
    model: "Huracan",
    year: 2021,
    available: true,
    description: "Exceptionally comfortable ride and stunning performance."
}

function display3() {
    var place = document.getElementById("submit3");

    for (value in lambo) {
        place.innerHTML = "Make: " + lambo.make + "<br>" +
            "Model: " + lambo.model + "<br>" +
            "Year: " + lambo.year + "<br>" +
            "Available: " + lambo.available + "<br>" +
            "About: " + lambo.description;
    }
};

function clear() {
    document.getElementById(".row").reset();
}   

It looks like you're attempting to use the HTMLFormElement reset method to revert the contents of the row, but that will not work because the element you are passing is a not an HTMLFormElement.看起来您正在尝试使用 HTMLFormElement 重置方法来恢复行的内容,但这不起作用,因为您传递的元素不是 HTMLFormElement。

While not recommended, you can "reset" the contents of your button the same way you set them:虽然不推荐,但您可以像设置按钮一样“重置”按钮的内容:

<button id="submit1" onclick="display1()">See Details</button>
<button onclick="clear1()">Clear</button>

function display1() {
    let place = document.getElementById("submit1");
    place.innerHTML = "Make: " + ...;
}

function clear1() {
    let place = document.getElementById("submit1");
    place.innerHTML = "See Details";
}

First off, reset won't work in this case.首先,在这种情况下重置将不起作用。 Secondly, you are not selecting the element itself.其次,您没有选择元素本身。

Also, when testing, I realized that onclick="clear()" doesn't fire the function, because it is a built-in function that is used to clear the console.此外,在测试时,我意识到onclick="clear()"不会触发 function,因为它是用于清除控制台的内置 function。

 var porsche = { make: "Porsche", model: "Panarama", year: 2020, available: true, description: "The Porsche Panamera is an excellent super luxury car." } function display1() { var place = document.getElementById("submit1"); for (value in porsche) { place.innerHTML = "Make: " + porsche.make + "<br>" + "Model: " + porsche.model + "<br>" + "Year: " + porsche.year + "<br>" + "Available: " + porsche.available + "<br>" + "About: " + porsche.description; } }; var ferrari = { make: "Ferrari", model: "Panarama", year: 2020, available: true, description: "Ranks near the top of super luxury cars, with a strong engine, well-balanced handling, and comfortable interior." } function display2() { var place = document.getElementById("submit2"); for (value in ferrari) { place.innerHTML = "Make: " + ferrari.make + "<br>" + "Model: " + ferrari.model + "<br>" + "Year: " + ferrari.year + "<br>" + "Available: " + ferrari.available + "<br>" + "About: " + ferrari.description; } }; var lambo = { make: "Lamborghini", model: "Huracan", year: 2021, available: true, description: "Exceptionally comfortable ride and stunning performance." } function display3() { var place = document.getElementById("submit3"); for (value in lambo) { place.innerHTML = "Make: " + lambo.make + "<br>" + "Model: " + lambo.model + "<br>" + "Year: " + lambo.year + "<br>" + "Available: " + lambo.available + "<br>" + "About: " + lambo.description; } }; function clear1() { document.getElementById("submit1").innerText="See Details"; } function clear2() { document.getElementById("submit2").innerText="See Details"; } function clear3() { document.getElementById("submit3").innerText="See Details"; }
 <div id="container"> <div class="row"> <div class="column"> <img src="images/Porsche.jpg"> <button onclick="display1()" id="submit1">See Details</button> <button onclick="clear1()" id="clear">Clear Selection</button> </div> <div class="column"> <img src="images/Ferrari.jpg"> <button onclick="display2()" id="submit2">See Details</button> <button onclick="clear2()" id="clear">Clear Selection</button> </div> <div class="column"> <img src="images/Lambo.jpg"> <button onclick="display3()" id="submit3">See Details</button> <button onclick="clear3()" id="clear">Clear Selection</button> </div> </div> </div>

By "refactoring" your code, I mean rewrite it without repeating it in multiple places (Think DRY - Don't Repeat Yourself ).通过“重构”你的代码,我的意思是重写它而不在多个地方重复它(Think DRY - Don't Repeat Yourself )。

In this example, I removed the onclick attributes in the HTML, used classes instead of IDs, and used a data-car attribute that is used in the JS:在此示例中,我删除了onclick属性,使用类而不是 ID,并使用了 JS 中使用的data-car属性:

 var cars = { porsche: { make: "Porsche", model: "Panarama", year: 2020, available: true, description: "The Porsche Panamera is an excellent super luxury car." }, ferrari: { make: "Ferrari", model: "Panarama", year: 2020, available: true, description: "Ranks near the top of super luxury cars, with a strong engine, well-balanced handling, and comfortable interior." }, lambo: { make: "Lamborghini", model: "Huracan", year: 2021, available: true, description: "Exceptionally comfortable ride and stunning performance." } }; // You can add the event listener once here (no `onclick` in the HTML) document.getElementById('container').addEventListener('click', function(e) { if (e.target.classList.contains('details-btn')) displayCarInfo(e.target); else if (e.target.classList.contains('clear-btn')) clear(e.target); }); function displayCarInfo(el) { // You can use a `data-car` attribute, declared in the HTML var carId = el.closest('[data-car]').getAttribute('data-car'), car = cars[carId]; el.innerHTML = "Make: " + car.make + "<br>" + "Model: " + car.model + "<br>" + "Year: " + car.year + "<br>" + "Available: " + car.available + "<br>" + "About: " + car.description; }; function clear(el) { var place = el.closest('[data-car]').querySelector('.details-btn'); place.innerHTML = 'See details'; }
 <div id="container"> <div class="row"> <div class="column" data-car="porsche"> <img src="images/Porsche.jpg"> <button class="details-btn">See Details</button> <button class="clear-btn">Clear Selection</button> </div> <div class="column" data-car="lambo"> <img src="images/Lambo.jpg"> <button class="details-btn">See Details</button> <button class="clear-btn">Clear Selection</button> </div> <div class="column" data-car="ferrari"> <img src="images/Ferrari.jpg"> <button class="details-btn">See Details</button> <button class="clear-btn">Clear Selection</button> </div> </div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM