我在几个按钮上有一个 .click()。每个 .click() 调用一个函数。该函数需要知道哪个按钮调用了它,以便它可以显示或隐藏不同的内容并更改其他按钮的样式。该函数有一个事件对象传递给它。
有没有办法获取事件对象的当前类?
最佳答案
$('myButton').click(function() {
var className = this.className; // This will give you the whole class string of the clicked element
var hasClass = $(this).hasClass('myClass'); // This will tell you (true or false) whether the clicked element has class 'myClass'
});
关于javascript - jQuery,发现 dom 对象的类是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3760885/