好吧,这应该很容易,但我似乎无法让它发挥作用。
我有一个按钮,当我单击它时,我希望它改变状态,但仅限于按下该按钮,就像任何操作系统 native 按钮一样。
这是我的代码...
$('button').live('mousedown', function(){
$(this).addClass('down');
}).live('mouseup', function(){
$(this).removeClass('down');
});
看起来很简单,对吧...但这行不通。当您单击它时,状态确实会发生变化...但它保持这种状态... mouseup 事件仅在您第二次单击它时起作用。
当我从 focus/blur 事件执行代码时,toggleClass 也会发生这种情况。
这里发生了什么,我怎样才能让它正常工作?
最佳答案
您应该考虑使用 CSS,因为它的显示速度比通过 Javascript 添加/删除类要快得多:
button:active { /* this is the mousedown pseudoelement */ }
input:focus, textarea:focus { /* this represents text input fields with focus */ }
关于jquery:按钮鼠标按下状态改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5026921/