Javascript jQuery检测子元素的类
我有一个按钮,单击该按钮时,我想显示另外两个按钮“取消”和“确认” 当添加Javascript jQuery检测子元素的类,javascript,jquery,Javascript,Jquery,我有一个按钮,单击该按钮时,我想显示另外两个按钮“取消”和“确认” 当添加o类时,将添加一个负的左边距,按钮将消失 $('.a_bttn_internal_action')。单击(函数(){ if($(this).children().find('.button')){ $(this).parent().find('.button').queue(函数(下一个){ $(this.addClass('o'); next(); }); $(this).parent().find('.confirm
o
类时,将添加一个负的左边距,按钮将消失
$('.a_bttn_internal_action')。单击(函数(){
if($(this).children().find('.button')){
$(this).parent().find('.button').queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.confirm.yes').delay(300).queue(函数(下一个){
$(this.removeClass('o');
next();
});
$(this).parent().find('.confirm.no').delay(100).queue(函数(下一个){
$(this.removeClass('o');
next();
});
}else if($(this.children().find('.confirm.no')){
$(this).parent().find('.confirm.yes').delay(300).queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.confirm.no').delay(100).queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.button').queue(函数(下一个){
$(this.removeClass('o');
next();
});
}
});代码>
li.o{
左边距:-800px;
}
您只需使用按钮
类检查孩子的数量即可
if ($(this).find('.button').length > 0) {
// there are some
}
您只需使用按钮
类检查子级的计数即可
if ($(this).find('.button').length > 0) {
// there are some
}
children()
获取所选元素的所有直接子元素find()
获取层次结构中所有的子元素
因此$(this).children()
将选择所有元素,然后find()
将查看这些的所有子元素,而不是查看本身
你想要的只是
$(this).find('.button').length
//or
$(this).find('.confirm.no').length
//or you could just find the li itself and then test for the class
var button = $(this).find('li');
if(button.is('.button')){
} else if(button.is(".confirm.no")){
}
另外请注意,不要针对children()
或find()
的返回进行测试,因为这两种方法都会返回一个新的jQuery对象,或者实际没有找到任何东西,因此结果将始终作为truthy进行测试
您也可以将click处理程序放在LI本身上,而不必担心将它们包装在锚标记中是无效的
$('ul.action li').click(function() {
var $this = $(this);
if($this.is('.button')){
} else if($this.is('.confirm.no')){
}
});
children()
获取所选元素的所有直接子元素find()
获取层次结构中所有的子元素
因此$(this).children()
将选择所有元素,然后find()
将查看这些的所有子元素,而不是查看本身
你想要的只是
$(this).find('.button').length
//or
$(this).find('.confirm.no').length
//or you could just find the li itself and then test for the class
var button = $(this).find('li');
if(button.is('.button')){
} else if(button.is(".confirm.no")){
}
另外请注意,不要针对children()
或find()
的返回进行测试,因为这两种方法都会返回一个新的jQuery对象,或者实际没有找到任何东西,因此结果将始终作为truthy进行测试
您也可以将click处理程序放在LI本身上,而不必担心将它们包装在锚标记中是无效的
$('ul.action li').click(function() {
var $this = $(this);
if($this.is('.button')){
} else if($this.is('.confirm.no')){
}
});
我相信这就是你所关心的。用于检查您可以使用的儿童
.find('.button').length
它将返回0或正整数,因此您可以在if中使用它。在代码中,第一个if始终为true,因此另一个if永远不会运行
$('.a_bttn_internal_action')。单击(函数(){
if($(this).find('.button').length){
$(this).parent().find('.button').queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.confirm.yes').delay(300).queue(函数(下一个){
$(this.removeClass('o');
next();
});
$(this).parent().find('.confirm.no').delay(100).queue(函数(下一个){
$(this.removeClass('o');
next();
});
}else if($(this).find('.confirm.no').length){
$(this).parent().find('.confirm.yes').delay(300).queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.confirm.no').delay(100).queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.button').queue(函数(下一个){
$(this.removeClass('o');
next();
});
}
});代码>
li.o{
左边距:-800px;
}
我相信这就是您所关心的。用于检查您可以使用的儿童
.find('.button').length
它将返回0或正整数,因此您可以在if中使用它。在代码中,第一个if始终为true,因此另一个if永远不会运行
$('.a_bttn_internal_action')。单击(函数(){
if($(this).find('.button').length){
$(this).parent().find('.button').queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.confirm.yes').delay(300).queue(函数(下一个){
$(this.removeClass('o');
next();
});
$(this).parent().find('.confirm.no').delay(100).queue(函数(下一个){
$(this.removeClass('o');
next();
});
}else if($(this).find('.confirm.no').length){
$(this).parent().find('.confirm.yes').delay(300).queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.confirm.no').delay(100).queue(函数(下一个){
$(this.addClass('o');
next();
});
$(this).parent().find('.button').queue(函数(下一个){
$(this.removeClass('o');
next();
});
}
});代码>
li.o{
左边距:-800px;
}
答案是exists()函数。
声明如下:
$(this.children().find('.button')
返回长度为零的JQuery对象,即使未选择任何元素
因此,if($(this).children().find('.button'))
总是被传递
您可以检查返回对象的长度,也可以使用JQueryexists()
函数
详情如下。答案是exis