一种检查是否单击了类的任何元素的方法

A way to check if any element of class is clicked

本文关键字:任何 方法 元素 单击 是否 一种 检查      更新时间:2023-09-26

有没有办法检查是否用jQuery点击了类的任何元素?我想检测是否有人点击了某个类的元素。

我想检测是否有人点击了某个类的元素。

您可以随时订阅.click()事件处理程序,当点击DOM元素时,它将被调用:

$('.someClass').click(function() {
    // here you could use "this" to get the DOM element that was clicked.
});

如果您想使用.data()函数跟踪DOM元素,那么您可以将一些元数据信息与它关联起来

$('.someClass').click(function() {
    var isClicked = $(this).data('clicked');
    if (!isClicked) {
        // it's the first time we are clicking on this element
        $(this).data('clicked', true);
    } else {
        // the user has already clicked on this element
    }
});
相关文章: