Javascript 如何使用关键字$(this)代替类选择器-jQuery?
我在jQuery中有以下代码-Javascript 如何使用关键字$(this)代替类选择器-jQuery?,javascript,jquery,Javascript,Jquery,我在jQuery中有以下代码- $(".new_makes_button").click(function(){ if($(".new_makes_button:checked").length == 0) { 那个么,我怎样才能将上面代码第二行中的类选择器“.new\u makes\u button”替换为$(this)?我只是在寻找正确的语法。在这里,我有几个类为“new_makes_button”的复选框,单击其中任何一个复选框,我将检查所有复选框是否都将被取消选中,然后运行以下
$(".new_makes_button").click(function(){
if($(".new_makes_button:checked").length == 0) {
那个么,我怎样才能将上面代码第二行中的类选择器“.new\u makes\u button”替换为$(this)?我只是在寻找正确的语法。在这里,我有几个类为“new_makes_button”的复选框,单击其中任何一个复选框,我将检查所有复选框是否都将被取消选中,然后运行以下代码。尝试以下操作:
if (!$(this).is(":checked")) {
...
}
试试这个:
if (!$(this).is(":checked")) {
...
}
您可以直接在单击的项目上查看checked属性,如下所示:
if (!$(this).prop("checked")) {
// code here for when the item is not checked
}
一旦有了DOM元素(这是您在
中的元素),就不需要再使用选择器了,因为您可以直接检查该项的属性。您可以直接在单击的项上查看选中的属性,如下所示:
if (!$(this).prop("checked")) {
// code here for when the item is not checked
}
一旦有了DOM元素(您在中有了它),就不需要再使用选择器,因为您可以直接检查该项的属性。这将检查是否选中了当前项。当前项是事件源元素<代码>$(此)
在回调中是对单个元素的引用
if(this.checked === false) {
}
但是,如果您想知道是否没有选中带有.new\u makes\u按钮
的元素,那么您可以使用jQuery
$(".new_makes_button").click(function() {
// $(this) at this point is a reference to a
// single element. To get Them all you may do this:
var total = 0;
$(".new_makes_button").each(function(index, element) {
if(this.checked === true) {
total++;
}
});
alert("Total checked item" + (total).toString());
});
如何替换第二行中的类选择器“.new_makes_button”
上面的代码是$(这个)
如果您确实想使用这个
对象,那么您可以,但是,不使用$(这个)
的语法是不正确的
要在该方法中使用此
,并使其引用数组。新建按钮
元素,可以使用绑定
更改其值。在本例中,this
将引用jQuery对象
$(".new_makes_button").click(function(e) {
// here `this` is a jquery object!
// no need to call the `$`
console.log(this.length);
}.bind($(".new_makes_button")));
最短可能解决方案
$(".new_makes_button").click(function(e) {
if(this.filter(':checked').length == 0) {
// handle condition
}
}.bind($(".new_makes_button")));
这将检查是否选中了当前项。当前项是事件源元素<代码>$(此)
在回调中是对单个元素的引用
if(this.checked === false) {
}
但是,如果您想知道是否没有选中带有.new\u makes\u按钮
的元素,那么您可以使用jQuery
$(".new_makes_button").click(function() {
// $(this) at this point is a reference to a
// single element. To get Them all you may do this:
var total = 0;
$(".new_makes_button").each(function(index, element) {
if(this.checked === true) {
total++;
}
});
alert("Total checked item" + (total).toString());
});
如何替换第二行中的类选择器“.new_makes_button”
上面的代码是$(这个)
如果您确实想使用这个
对象,那么您可以,但是,不使用$(这个)
的语法是不正确的
要在该方法中使用此
,并使其引用数组。新建按钮
元素,可以使用绑定
更改其值。在本例中,this
将引用jQuery对象
$(".new_makes_button").click(function(e) {
// here `this` is a jquery object!
// no need to call the `$`
console.log(this.length);
}.bind($(".new_makes_button")));
最短可能解决方案
$(".new_makes_button").click(function(e) {
if(this.filter(':checked').length == 0) {
// handle condition
}
}.bind($(".new_makes_button")));
在jQuery中,所作用的元素通常被传递给jQuery函数的函数参数,如下所示。可以在函数参数中使用$(this)将其生成jQuery元素对象, 例: $('.new_makes_button')。单击(函数(){ $(this).animate({paddingLeft:$(this).width()},200)
})) 在jQuery中,所作用的元素通常被传递给jQuery函数的函数参数,如下所示。可以在函数参数中使用$(this)将其生成jQuery元素对象, 例: $('.new_makes_button')。单击(函数(){ $(this).animate({paddingLeft:$(this).width()},200)
})) 你的意思是
if($(this).not(“:checked”)
?你的意思是if($(this).not(“:checked”)
?好的,我理解,但是你如何转换这个代码$(“.new_makes_button”)。点击(函数(){if($(“.new_makes_button:checked”)。长度=0){转换为在if条件下使用“this”而不是类选择器的函数?好的,我理解,但如何将此代码$(“.new_makes_按钮”)。单击(函数(){if($(“.new_makes_按钮:选中”)。长度==0{转换为在if条件下使用“this”而不是类选择器的函数?