[在运行功能之前已单击检查元素
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[在运行功能之前已单击检查元素相关的知识,希望对你有一定的参考价值。
我有一个用户在关注任务中选择(单击)的多个元素。目前,我有一些代码,其中的元素在单击时变为红色。完成后,用户将单击“下一步”以查看其他任务。至关重要的是,他们必须在继续之前进行选择。
因此,我需要检查用户是否已选择元素之一。
[如果某人在单击“下一步”之前尚未选择元素之一,那么我会写一条错误消息。但是首先,我在检查是否首先单击某个元素时遇到了麻烦。最好,我还将检查哪个元素是否被单击。
提前感谢您的帮助!
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style media="screen">
.buttons {
width: 150px;
height: 50px;
border: solid 2px white;
text-align: center;
color: black;
cursor: pointer;
}
#buttonGallery {
margin: 10px;
padding: 10px;
border: solid 2px black;
width: 155px;
}
#button_1 {
background-color: #ffcc66;
}
#button_2 {
background-color: #99ffff;
}
.buttons:hover {
background-color: red !important;
border: solid 3px black !important;
}
.selected {
background-color: red !important;
border: solid 3px black !important;
}
#next {
background-color: gray;
margin: 10px;
width: 150px;
height: 50px;
border: solid 2px white;
text-align: center;
color: black;
cursor: pointer;
}
</style>
</head>
<body>
<div id="buttonGallery">
<div id="button_1" class="buttons">
<p>button_1</p>
</div>
<div id="button_2" class="buttons">
<p>button_2</p>
</div>
</div>
<div id="next">
<p>next</p>
</div>
<script type="text/javascript">
$('.buttons').click(function() {
$('.buttons').removeClass("selected");
$(this).toggleClass("selected");
});
$('#next').click(function() {
// check that one of the elements has been clicked
// if an element has not been clicked, then show error message
// console.log(which element was clicked)
$('#buttonGallery').hide();
$('#next').hide();
});
</script>
</body>
</html>
答案
如果至少单击了一个元素,则该buttons
类元素将具有类selected
。因此,我们可以使用如下逻辑:
未选择任何元素,如果
buttons
没有任何selected
类,例如:$('.buttons.selected').length === 0
[一个元素,如果
buttons
具有selected
类,例如:$('.buttons.selected').length > 0 // Or, simply the else part in the demo.
要获得单击的
id
的buttons
,我们可以简单地使用:var id = $('.buttons.selected').attr('id');
$('.buttons').click(function() {
$('.buttons').removeClass("selected");
$(this).toggleClass("selected");
});
$('#next').click(function() {
// check that one of the elements has been clicked
// if an element has not been clicked, then show error message
if ($('.buttons.selected').length === 0) {
console.log('Please select one element first.')
} else {
// which element was clicked
var id = $('.buttons.selected').attr('id');
console.log(id + ' was clicked');
$('#buttonGallery').hide();
$('#next').hide();
}
});
.buttons{width:150px;height:50px;border:solid 2px #fff;text-align:center;color:#000;cursor:pointer}
#buttonGallery{margin:10px;padding:10px;border:solid 2px #000;width:155px}
#button_1{background-color:#fc6}
#button_2{background-color:#9ff}
.buttons:hover{background-color:red!important;border:solid 3px #000!important}
.selected{background-color:red!important;border:solid 3px #000!important}
#next{background-color:gray;margin:10px;width:150px;height:50px;border:solid 2px #fff;text-align:center;color:#000;cursor:pointer}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="buttonGallery">
<div id="button_1" class="buttons">
<p>button_1</p>
</div>
<div id="button_2" class="buttons">
<p>button_2</p>
</div>
</div>
<div id="next">
<p>next</p>
</div>
另一答案
单击下一个按钮后,您需要检查按钮是否具有selected
类,如果没有选定的类,则显示错误消息。
$('#error').hide();
$('.buttons').click(function() {
$('#error').hide();
$('.buttons').removeClass("selected");
$(this).toggleClass("selected");
});
$('#next').click(function() {
// check that one of the elements has been clicked
// if an element has not been clicked, then show error message
// console.log(which element was clicked)
const isSelected = $('.buttons').hasClass('selected')
if (!isSelected) {
$('#error').show()
return false;
}
const selectedButton = $('.buttons.selected');
$('#buttonGallery').hide();
$('#next').hide();
});
.buttons {
width: 150px;
height: 50px;
border: solid 2px white;
text-align: center;
color: black;
cursor: pointer;
}
#buttonGallery {
margin: 10px;
padding: 10px;
border: solid 2px black;
width: 155px;
}
#button_1 {
background-color: #ffcc66;
}
#button_2 {
background-color: #99ffff;
}
.buttons:hover {
background-color: red !important;
border: solid 3px black !important;
}
.selected {
background-color: red !important;
border: solid 3px black !important;
}
#next {
background-color: gray;
margin: 10px;
width: 150px;
height: 50px;
border: solid 2px white;
text-align: center;
color: black;
cursor: pointer;
}
.error {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="buttonGallery">
<div id="button_1" class="buttons">
<p>button_1</p>
</div>
<div id="button_2" class="buttons">
<p>button_2</p>
</div>
</div>
<div id="next">
<p>next</p>
</div>
<div class="error" id="error">Nothing selected</div>
以上是关于[在运行功能之前已单击检查元素的主要内容,如果未能解决你的问题,请参考以下文章