How to find target is checkbox ?
$(document).click(function(e) {
// Do things
});
how to find this ? if it's check box not need to proceed this events ?
How to find target is checkbox ?
$(document).click(function(e) {
// Do things
});
how to find this ? if it's check box not need to proceed this events ?
You can detect click on all elements (*
) so you can use $(this)
.
$("*").on("click", function (e) {
if ($(this).attr("type") === "checkbox") {
alert("Checkbox click!");
}
});
If you want to detect clicks only on checkboxes use input[type='checkbox']
selector.
$("input[type='checkbox']").click(function(){
...
});
Like @XAOPT said we also can detect the clicks on document
and then verifying if it's a checkbox using e.srcElement
.
$(document).click(function(e) {
if(e.srcElement == "checkbox") {
alert("Checkbox click!");
}
});
或者
$(document).click(function(e) {
if(e.srcElement.type == 'checkbox')
alert('checkbox');
});
$(document).on('click', ':checkbox', function() {
// do things
});
除非有理由您需要对文档进行通用单击并自己进行授权...
jQuery 选择器可以为您做到这一点:
$("input[type=checkbox]").click(function(){
...
});
你可以使用这个 $("input[type='checkbox']").click(function(){
});