gpt4 book ai didi

javascript - 在 jQuery 或 Javascript 中切换按钮选择的大小写

转载 作者:行者123 更新时间:2023-11-28 02:54:45 25 4
gpt4 key购买 nike

有谁知道如何减少我的代码或使其更容易?该代码旨在选择三十个按钮中的三个按钮。所以我可以通过选择三个我感兴趣的按钮来查看一些信息。实际上代码到目前为止包含 8 个选择按钮,但我需要制作 30 个按钮。

--------------------我的代码-------------------- ------

<script>

$(document).ready(function(){
$("#button2").click(function(){
$("#myNav1").fadeIn(1000);
});
$(".closebtn").click(function(){
$("#myNav1").animate({width: '0px'});
});
var counter = 0;
$("button").bind("click",function(){
var btnid = this.id;
if ( counter < 4) {
switch (btnid){ case "btn1": switch (counter){
case 0 : $("#img1").attr("src","icon/1.jpg");counter ++; break;
case 1 : $("#img2").attr("src","icon/1.jpg");counter ++; break;
case 2 : $("#img3").attr("src","icon/1.jpg");counter ++; break;
case 3 : alert("Please Select result button");counter = 0 ; break;
} break;
case "btn2": switch (counter){
case 0 : $("#img1").attr("src","icon/2.jpg");counter ++; break;
case 1 : $("#img2").attr("src","icon/2.jpg");counter ++; break;
case 2 : $("#img3").attr("src","icon/2.jpg");counter ++; break;
case 3 : alert("Please Select result button"); counter = 0 ; break;
} break;
case "btn3": switch (counter){
case 0 : $("#img1").attr("src","icon/3.jpg");counter ++;break;
case 1 : $("#img2").attr("src","icon/3.jpg");counter ++;break;
case 2 : $("#img3").attr("src","icon/3.jpg");counter ++;break;
case 3 : alert("Please Select result button");counter = 0 ;break;
} break;
case "btn4": switch (counter){
case 0 : $("#img1").attr("src","icon/4.jpg");counter ++;break;
case 1 : $("#img2").attr("src","icon/4.jpg");counter ++;break;
case 2 : $("#img3").attr("src","icon/4.jpg");counter ++;break;
case 3 : alert("Please Select result button");counter = 0 ; break;
} break;
case "btn4": switch (counter){
case 0 : $("#img1").attr("src","icon/4.jpg");counter ++;break;
case 1 : $("#img2").attr("src","icon/4.jpg");counter ++;break;
case 2 : $("#img3").attr("src","icon/4.jpg");counter ++;break;
case 3 : alert("Please Select result button");counter = 0 ; break;
} break;
case "btn5": switch (counter){
case 0 : $("#img1").attr("src","icon/5.jpg");counter ++;break;
case 1 : $("#img2").attr("src","icon/5.jpg");counter ++;break;
case 2 : $("#img3").attr("src","icon/5.jpg");counter ++;break;
case 3 : alert("Please Select result button");counter = 0 ; break;
} break;
case "btn6": switch (counter){
case 0 : $("#img1").attr("src","icon/6.jpg");counter ++;break;
case 1 : $("#img2").attr("src","icon/6.jpg");counter ++;break;
case 2 : $("#img3").attr("src","icon/6.jpg");counter ++;break;
case 3 : alert("Please Select result button");counter = 0 ; break;
} break;
case "btn7": switch (counter){
case 0 : $("#img1").attr("src","icon/7.jpg");counter ++;break;
case 1 : $("#img2").attr("src","icon/7.jpg");counter ++;break;
case 2 : $("#img3").attr("src","icon/7.jpg");counter ++;break;
case 3 : alert("Please Select result button");counter = 0 ; break;
} break;
case "btn8": switch (counter){
case 0 : $("#img1").attr("src","icon/8.jpg");counter ++;break;
case 1 : $("#img2").attr("src","icon/8.jpg");counter ++;break;
case 2 : $("#img3").attr("src","icon/8.jpg");counter ++;break;
case 3 : alert("Please Select result button");counter = 0 ; break;
} break;
case "btn9": switch (counter) {
case 0: break;
case 1: $("#img1").attr("src","");counter --; break;
case 2: $("#img2").attr("src","");counter --; break;
case 3: $("#img3").attr("src","");counter --; break;
} break;
}
}
else{
alert("Please Select result button");
counter = 0 ;
}
});
});
</script>
</head>
<body>

<button id="button2">HTML Start Button</button>
<button id="button3">HTML Reset Button</button>
<div class="btnCl">
<button id="btn1">11</button>
<button id="btn2">22</button>
<button id="btn3">33</button>
<button id="btn4">44</button>
<button id="btn5">55</button>
<button id="btn6">66</button>
<button id="btn7">77</button>
<button id="btn8">88</button>
<button id="btn9">return</button>
</div>

<div id="myNav1" class="overlay">
<a href="javascript:void(0)" class="closebtn" >&times;</a>
<div class="overlay-content">
<h1>Selected Contents</h1>
<div class="imgCl">
<img id="img1" src="" alt=""></img>
<img id="img2" src="" alt=""></img>
<img id="img3" src="" alt=""></img>
</div>
</br>
</div>
</div>

最佳答案

最好有像这样通用的东西。为限制创建变量。例如 buttonEnd = 9;。然后在代码中使用它们。您可以根据按钮的数量进行更改。

var counter = 0;
var buttonEnd = 9;
$(document).ready(function() {
$("#button2").click(function() {
$("#myNav1").fadeIn(1000);
});
$(".closebtn").click(function() {
$("#myNav1").animate({
width: '0px'
});
});
$("button").bind("click", function() {
var btnid = Number(this.id.replace('btn', ''));
if (counter < 4) {
if (btnid < buttonEnd) {
if (counter == 3) {
alert("Please Select result button");
counter = 0;
} else {
$("#img" + (counter + 1)).attr("src", "icon/" + btnid + ".jpg");
counter++;
}

} else {
$("#img" + (counter)).attr("src", "");
counter--;
}
} else {
alert("Please Select result button");
counter = 0;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="button2">HTML Start Button</button>
<button id="button3">HTML Reset Button</button>
<div class="btnCl">
<button id="btn1">11</button>
<button id="btn2">22</button>
<button id="btn3">33</button>
<button id="btn4">44</button>
<button id="btn5">55</button>
<button id="btn6">66</button>
<button id="btn7">77</button>
<button id="btn8">88</button>
<button id="btn9">return</button>
</div>

<div id="myNav1" class="overlay">
<a href="javascript:void(0)" class="closebtn">&times;</a>
<div class="overlay-content">
<h1>Selected Contents</h1>
<div class="imgCl">
<img id="img1" src="" alt=""></img>
<img id="img2" src="" alt=""></img>
<img id="img3" src="" alt=""></img>
</div>
</br>
</div>
</div>

关于javascript - 在 jQuery 或 Javascript 中切换按钮选择的大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586730/

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