gpt4 book ai didi

javascript - 单击按钮时添加到无序列表

转载 作者:行者123 更新时间:2023-12-01 08:30:17 25 4
gpt4 key购买 nike

我对 JQuery 还很陌生,我正在尝试制作这个餐厅订购系统。我试图做到这一点,以便当您单击食物选择中的按钮时。它将食物的名称和价格添加到无序列表中,以便您可以在侧面看到您选择的食物和价格的列表。

var coll = $(".collapsible");

for (var i = 0; i < coll.length; i++) {

coll[i].addEventListener("click", function() {

this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}


$(".button.table").click(function(){
var price = $(this).attr("id");
var name = $(this).attr("name");
$(".orders li").html("<li>" + price + "</li>");
});
/* Splits Screen */
html, body {
height: 100%;
padding: 0;
margin: 0;
}

/* Choice section */
.choice {
width: 70%;
height: 100%;
float: left;
padding: 0 25px 0 25px;
background-color: gray;
}


/* Order List */
.order {
width: 30%;
height: 100%;
float: left;
}

h1{
font-size: 50px;
font-weight: 700;
color: black;
text-align: center;
}

#appetizer{
width: 100%;
}
#entree{
width: 100%;
}
#dessert{
width: 100%;
}
#drinks{
width: 100%;
}


.collapsible {
background-color: lightgray;
border-radius: 5px;
border: 2px solid black;
margin: 5px;
color: black;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: center;
outline: none;
font-size: 30px;

}

.content {
padding: 0;
display: none;
overflow: hidden;
background-color: white;
}

.food{
margin: auto;
width: 100%;
height: 55px;
font-size: 20px;
}

ul li{
list-style: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- BootStrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="styles2.css">

<title>Order</title>
</head>
<body>
<div class="choice">
<h1>Choices</h1>

<button class="collapsible">Appetizers</button>
<div class="content">
<button class="food" name="Wings" id="8">Wings</button>
<button class="food" name="Calamari" id="12">Calamari</button>
<button class="food" name="Cheese Dip" id="7">Cheese Dip</button>
</div>

<button class="collapsible">Entrees</button>
<div class="content">
<button class="food">Wings</button>
<button class="food">Wings</button>
<button class="food">Wings</button>
</div>

<button class="collapsible">Desserts</button>
<div class="content">
<button class="food">Wings</button>
<button class="food">Wings</button>
<button class="food">Wings</button>
</div>

<button class="collapsible">Drinks</button>
<div class="content">
<button class="food">Wings</button>
<button class="food">Wings</button>
<button class="food">Wings</button>
</div>
</div>


<div class="orderList">
<h1>Order:</h1>
<div class="list-group">
<ul class= "orders">
<li></li>
</ul>
</div>
</div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="tables.js"></script>
</body>
</html>

最佳答案

我编辑了你的代码,现在工作正常了但是1- ID 不是数字,您可以使用数据- 获取信息,例如价格2-Jquery有简短的命令,如果你编写JQUERY而不是纯javascript,你可以使用它例如你写这个

for (var i = 0; i < coll.length; i++) {

coll[i].addEventListener("click", function() {

this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}

但是我写了这个

  $(".collapsible").click(function(){

this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});

3-在Jquery中Selector非常重要你写这个

$(".button.table")

你没有 button 类,也没有表类这个选择器返回任何东西

    
$(".collapsible").click(function(){

this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});



$("button.food").click(function(){
var price = $(this).attr("data-price");
var name = $(this).attr("name");
$(".orders").append("<li>" +name+":"+ price + "</li>");
});
/* Splits Screen */
html, body {
height: 100%;
padding: 0;
margin: 0;
}

/* Choice section */
.choice {
width: 70%;
height: 100%;
float: left;
padding: 0 25px 0 25px;
background-color: gray;
}


/* Order List */
.order {
width: 30%;
height: 100%;
float: left;
}

h1{
font-size: 50px;
font-weight: 700;
color: black;
text-align: center;
}

#appetizer{
width: 100%;
}
#entree{
width: 100%;
}
#dessert{
width: 100%;
}
#drinks{
width: 100%;
}


.collapsible {
background-color: lightgray;
border-radius: 5px;
border: 2px solid black;
margin: 5px;
color: black;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: center;
outline: none;
font-size: 30px;

}

.content {
padding: 0;
display: none;
overflow: hidden;
background-color: white;
}

.food{
margin: auto;
width: 100%;
height: 55px;
font-size: 20px;
}

ul li{
list-style: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- BootStrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="styles2.css">

<title>Order</title>
</head>
<body>
<div class="choice">
<h1>Choices</h1>

<button class="collapsible">Appetizers</button>
<div class="content">
<button class="food" name="Wings" data-price="8">Wings</button>
<button class="food" name="Calamari" data-price="12">Calamari</button>
<button class="food" name="Cheese Dip" data-price="7">Cheese Dip</button>
</div>

<button class="collapsible">Entrees</button>
<div class="content">
<button class="food">Wings</button>
<button class="food">Wings</button>
<button class="food">Wings</button>
</div>

<button class="collapsible">Desserts</button>
<div class="content">
<button class="food">Wings</button>
<button class="food">Wings</button>
<button class="food">Wings</button>
</div>

<button class="collapsible">Drinks</button>
<div class="content">
<button class="food">Wings</button>
<button class="food">Wings</button>
<button class="food">Wings</button>
</div>
</div>


<div class="orderList">
<h1>Order:</h1>
<div class="list-group">
<ul class= "orders">

</ul>
</div>
</div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="tables.js"></script>
</body>
</html>

关于javascript - 单击按钮时添加到无序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61220588/

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