[英]How to select a specific div with specific class jQuery?
I have a circle, when I clicked on it 2 options showed up. 我有一个圆圈,当我单击它时,显示两个选项。
<div class="col-sm-2" style="display: block;">
<span class="fa fa-mobile circle-icon fa-5x client" style="background-color: rgb(75, 183, 232);">
</span>
<div class="line" style="display: none;"></div>
<div class="options" style="display: block;">
<input type="text" class="search" name="">
<i class="fa fa-plus float-right"></i>
<p class="option" id="Dualstack-Pvt">
Dualstack-Pvt
<i class="fa fa-info-circle float-right"></i>
</p>
<p class="option" id="Dualstack-Pvt-2">
Dualstack-Pvt-2
<i class="fa fa-info-circle float-right"></i>
</p>
</div>
<div class="selected" style="display: none;"></div>
</div>
I want to do something when one of the option clicked. 单击其中一个选项时,我想做一些事情。
I can't seem to select it. 我似乎无法选择它。
This is what I have now. 这就是我现在所拥有的。
$('.' + selector).on("click", function() {
if (steps.indexOf(selector) != -1) {
nextStep = steps[steps.indexOf(selector) + 1];
}
let self = $(this);
$('div.options').fadeOut('fast');
self.animate({
backgroundColor: fadeColor
}, 1400);
self.next().next('.options').slideDown(1000);
console.log(self)
self.find('.options').find('option').one("click", function(event) {
console.log('%c -------->> HERE <<--------', "color: green;");
});
});
This is my selection 这是我的选择
self.find('.options').find('option').one("click", function(event) {
self.find('。options')。find('option')。one(“ click”,function(event){
I thought find() will find the matching class ... No ? 我以为find()会找到匹配的类...否?
Please help me understand why this is not working ... 请帮助我了解为什么这不起作用...
In the query you have here: 在查询中,您有:
self.find('.options').find('option')
The section self.find('.options')
will look for elements that are the class option
that are the children of self
. self.find('.options')
将查找作为self
的子级的类option
元素。 In this case since self
is a <span>
with no children so the selector will not work. 在这种情况下,由于
self
是没有子代的<span>
,因此选择器将不起作用。 Note you are already doing this when you used self.next().next('.options')
. 注意,当您使用
self.next().next('.options')
时,您已经在执行此操作。 Next the .find('option')
part will look for the tag <option>
and not the class option
, so change this to .option
. 接下来,
.find('option')
部分将查找标记<option>
而不是类option
,因此将其更改为.option
。 With those fixes you will have: 有了这些修复程序,您将具有:
self.siblings('.options').find('.option').one("click", function(event) {
console.log('%c -------->> HERE <<--------', "color: green;");
});
Snippet example 片段示例
$('.fa-mobile').on("click", function() { let self = $(this); $('div.options').fadeOut('fast'); self.next().next('.options').slideDown(1000); self.siblings('.options').find('.option').one("click", function(event) { console.log('%c -------->> HERE <<--------', "color: green;"); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-sm-2" style="display: block;"> <span class="fa fa-mobile circle-icon fa-5x client" style="background-color: rgb(75, 183, 232);">Circle </span> <div class="line" style="display: none;"></div> <div class="options" style="display: block;"> <input type="text" class="search" name=""> <i class="fa fa-plus float-right"></i> <p class="option" id="Dualstack-Pvt"> Dualstack-Pvt <i class="fa fa-info-circle float-right"></i> </p> <p class="option" id="Dualstack-Pvt-2"> Dualstack-Pvt-2 <i class="fa fa-info-circle float-right"></i> </p> </div> <div class="selected" style="display: none;"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.