gpt4 book ai didi

javascript - 如何使多个按钮 "click to reveal"在 javascript 中工作?

转载 作者:行者123 更新时间:2023-12-01 00:22:11 25 4
gpt4 key购买 nike

我正在尝试制作一个相对简单的 JavaScript 文件,该文件允许我单击 5 个按钮之一,信息就会显示在屏幕上。目前,我让它只显示一次,但其他的却没有出现。

简单地说,目标就是拥有它,这样如果我单击“选择 1”,则会出现文本“测试 1”、“选择 2”和“测试 2”,依此类推...

任何帮助将不胜感激!

<!DOCTYPE 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">

<title>Bootstrap 4, from LayoutIt!</title>

<meta name="description" content="Source code generated using layoutit.com">
<meta name="author" content="LayoutIt!">

<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

</head>
<body>

<div class="container-fluid">
<div class="row">
<div class="col-md-6 card">
<div class="row">
<div class="col-md-2">
<h3>
1
</h3>
</div>
<div class="col-md-10">
<h3 class="text-center">
Choose Your Selection
</h3>
</div>
</div>
<div class="row">
<div class="col-md-12">

<button type="button1" class="btn btn-success btn-lg btn-block">
Selection #1
</button>
<button type="button2" class="btn btn-success btn-lg btn-block">
Selection #2
</button>
<button type="button3" class="btn btn-success btn-lg btn-block">
Selection #3
</button>
<button type="button4" class="btn btn-success btn-lg btn-block">
Selection #4
</button>
</div>
</div>
</div>
<div class="col-md-6 card">
<div class="row">
<div class="col-md-2">
<h3>
2
</h3>
</div>
<div class="col-md-10">
<h3 class="text-center">
Choose Your Option
</h3>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p id="select1" class="select">test 1</p>
<p id="select2" class="select">test 2</p>
<p id="select3" class="select">test 3</p>
<p id="select4" class="select">test 4</p>
</div>
</div>
</div>
</div>
</div>

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/scripts.js"></script>
<script type="text/javascript"> var tag = document.getElementById('select1'); var button = document.querySelector('button[type="button1"]'); button.addEventListener('click', function(){ tag.classList.toggle('selecton'); }); </script>
<script type="text/javascript"> var tag = document.getElementById('select2'); var button = document.querySelector('button[type="button2"]'); button.addEventListener('click', function(){ tag.classList.toggle('selecton'); }); </script>
<script type="text/javascript"> var tag = document.getElementById('select3'); var button = document.querySelector('button[type="button3"]'); button.addEventListener('click', function(){ tag.classList.toggle('selecton'); }); </script>
<script type="text/javascript"> var tag = document.getElementById('select4'); var button = document.querySelector('button[type="button4"]'); button.addEventListener('click', function(){ tag.classList.toggle('selecton'); }); </script>
</body>
</html>
.container-fluid {
width:40%;
padding: 50px 0;
}
.card {
background-color: rgb(24, 26, 27);
border-top-color: rgba(102, 102, 102, 0.13);
border-right-color: rgba(102, 102, 102, 0.13);
border-bottom-color: rgba(102, 102, 102, 0.13);
border-left-color: rgba(102, 102, 102, 0.13);
border-radius: 20px;
padding: 20px;
border: 5px solid;
}


body {background-color: #000;}


.select {display:none;}

.selecton {display: block;}

最佳答案

   <body>

<div class="container-fluid">
<div class="row">
<div class="col-md-6 card">
<div class="row">
<div class="col-md-2">
<h3>
1
</h3>
</div>
<div class="col-md-10">
<h3 class="text-center">
Choose Your Selection
</h3>
</div>
</div>
<div class="row">
<div class="col-md-12">

<button type="button1" class="btn btn-success btn-lg btn-block" data-btn='btn-1'>
Selection #1
</button>
<button type="button2" class="btn btn-success btn-lg btn-block" data-btn='btn-2'>
Selection #2
</button>
<button type="button3" class="btn btn-success btn-lg btn-block" data-btn='btn-3'>
Selection #3
</button>
<button type="button4" class="btn btn-success btn-lg btn-block" data-btn='btn-4'>
Selection #4
</button>
</div>
</div>
</div>
<div class="col-md-6 card">
<div class="row">
<div class="col-md-2">
<h3>
2
</h3>
</div>
<div class="col-md-10">
<h3 class="text-center">
Choose Your Option
</h3>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p id="select1" class="select">test 1</p>
<p id="select2" class="select">test 2</p>
<p id="select3" class="select">test 3</p>
<p id="select4" class="select">test 4</p>
</div>
</div>
</div>
</div>
</div>

删除按钮类型“button1”它必须只是“按钮”

js:

 var buttons = document.querySelectorAll('.btn-block');
buttons.forEach(function(button, index){
button.addEventListener('click', function(){
if(button.dataset.btn===`btn-${index}`){
document.getElementById(`select${index}`).classList.toggle('selecton');
}
});
});

关于javascript - 如何使多个按钮 "click to reveal"在 javascript 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317776/

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