繁体   English   中英

使用 innerHTML javascript 从数组中删除特定元素

[英]Delete specific element from array with innerHTML javascript

我需要在 javascript 中创建一个简单的项目,我们需要创建一个包含 javascript 对象的单个库并让用户添加新书。 我在 html 中使用表单标签请求用户数据并创建了新对象,并将它们存储在一个名为 library 的单个数组中。 这些书在 DOM 中显示没有问题,问题是我需要一个删除特定书的按钮,我创建了一个按钮,但它只删除数组中的第一本书。 我希望你能帮助我。

HTML:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./styles.css" />
    <title>Document</title>
</head>
<body>
    <h1>My Library</h1>
    <input id="title" type="text" placeholder="Book Title">
    <input id="author" type="text" placeholder="Book Author">
    <input id="date" type="text" placeholder="Publish Date">

    <select id="read" name="read">
      <option value="yes">yes</option>
      <option value="no">no</option>
    </select> 

    <input type="button" value="New Book" onclick="add_book()">

    <div id="display"></div>
  
    <script src="app.js"></script>
</body>
</html>


------------------------------------------------------------------------------------------------


JAVASCRIPT:

var library = [];

var title_input = document.getElementById("title");
var author_input = document.getElementById("author");
var date_input = document.getElementById("date");
var read_input = document.getElementById("read");

function Book(title, author, date, read) {
    this.title = title;
    this.author = author;
    this.date = date
    this.read = read
};

function add_book() {
    var newBook = new Book(title_input, author_input, date_input, read_input)
    library.push(`Title: ${newBook.title.value} <br>`+`Author: ${newBook.author.value} <br>`+
    `Realease date: ${newBook.date.value} <br>`+`Readed: ${newBook.read.value} <br>` )
    show_library(); 
};

function delete_book(arr, elem){
    index = arr.indexOf(elem);
    arr.splice(elem,1);
    show_library(); 
}

function show_library() {
    document.getElementById("display").innerHTML = "";
    for(i = 0; i<library.length; i++){
        document.getElementById("display").innerHTML += library[i]+
        '<button onclick="delete_book(library, library[i]);">Delete</button><br>';
    }
};




您已将library[i]作为纯文本写入 DOM。 这不会引用循环中的变量i

您可以简单地将索引编写为参数,然后直接在 function delete_book中使用它。

 var library = []; var title_input = document.getElementById("title"); var author_input = document.getElementById("author"); var date_input = document.getElementById("date"); var read_input = document.getElementById("read"); function Book(title, author, date, read) { this.title = title; this.author = author; this.date = date this.read = read }; function add_book() { var newBook = new Book(title_input, author_input, date_input, read_input) library.push(`Title: ${newBook.title.value} <br>`+`Author: ${newBook.author.value} <br>`+ `Realease date: ${newBook.date.value} <br>`+`Readed: ${newBook.read.value} <br>` ) show_library(); }; function delete_book(arr, index){ arr.splice(index, 1); show_library(); } function show_library() { document.getElementById("display").innerHTML = ""; for(i = 0; i<library.length; i++){ document.getElementById("display").innerHTML += library[i]+ `<button onclick="delete_book(library, ${i});">Delete</button><br>`; } };
 <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="./styles.css" /> <title>Document</title> </head> <body> <h1>My Library</h1> <input id="title" type="text" placeholder="Book Title"> <input id="author" type="text" placeholder="Book Author"> <input id="date" type="text" placeholder="Publish Date"> <select id="read" name="read"> <option value="yes">yes</option> <option value="no">no</option> </select> <input type="button" value="New Book" onclick="add_book()"> <div id="display"></div> <script src="app.js"></script> </body> </html>

暂无
暂无

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

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