gpt4 book ai didi

javascript - 如何在页面上提交新元素? (jQuery)

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:25 25 4
gpt4 key购买 nike

为学校做一个元素。我必须能够将杂货元素添加到列表中,并在包含新元素的框中使用控件“检查”和“删除”。 list 上已有 4 项供引用。我已经能够添加盒子,里面有食物,但我似乎无法在里面添加按钮,以及与列表中其他元素相同的字体和样式。

Screenshot of what I'm getting when I add itemjQuery

function addItem(){
$('#js-shopping-list-form').submit(function(event){
event.preventDefault();
const textInput = $('#shopping-list-entry').val();
$('ul').append('<li>' + textInput + '</li>')
});
};
$(addItem);

CSS

* {
box-sizing: border-box;
}

body {
font-family: 'Roboto', sans-serif;
}

button, input[type="text"] {
padding: 5px;
}

button:hover {
cursor: pointer;
}

#shopping-list-item {
width: 250px;
}

.container {
max-width: 600px;
margin: 0 auto;
}

.shopping-list {
list-style: none;
padding-left: 0;
}

.shopping-list > li {
margin-bottom: 20px;
border: 1px solid grey;
padding: 20px;
}

.shopping-item {
display: block;
color: grey;
font-style: italic;
font-size: 20px;
margin-bottom: 15px;
}

.shopping-item__checked {
text-decoration: line-through;
}

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<title>Shopping List</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

<link rel="stylesheet" href="main.css">
</head>
<body>

<div class="container">
<h1>Shopping List</h1>

<form id="js-shopping-list-form">
<label for="shopping-list-entry">Add an item</label>
<input type="text" name="shopping-list-entry" id="shopping-list-entry" placeholder="e.g., broccoli">
<button type="submit">Add item</button>
</form>

<ul class="shopping-list">
<li>
<span class="shopping-item">apples</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
<li>
<span class="shopping-item">oranges</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
<li>
<span class="shopping-item shopping-item__checked">milk</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
<li>
<span class="shopping-item">bread</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
</ul>
</div>

<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script src="index.js"></script>

</body>
</html>

最佳答案

你的 addItem()方法需要包含所有 HMTL 以生成新的格式化元素,而不仅仅是 <li>元素。

  function addItem(){
$('#js-shopping-list-form').submit(function(event){
event.preventDefault();
const textInput = $('#shopping-list-entry').val();
$('ul').append('<li><span class="shopping-item">' + textInput + '</li></span> <div class="shopping-item-controls"> <button class="shopping-item-toggle"> <span class="button-label">check</span> </button> <button class="shopping-item-delete"> <span class="button-label">delete</span> </button> </div>')
});

关于javascript - 如何在页面上提交新元素? (jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58637418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com