如果用户的鼠标离开了两个 html 元素(一个文本字段以及一个 span block ),我想发出警报。我在jquery中怎么说AND,我徒劳地尝试了这样的事情:
if ($('textarea.commentField') && $('span.loginPrompt')).mouseout(function() {
alert('something');
});
最佳答案
您无法在本地执行此操作,因为这需要一些跑腿工作。
这样的方法可行,但必须有更好的解决方案:
var state = {a:false,b:false};
$('.a').mouseenter(enterA);
$('.a').mouseout(exitA);
$('.b').mouseenter(enterB);
$('.b').mouseout(exitB);
function enterA(){ state.a = true }
function exitA(){ state.a = false }
function enterB(){ state.b = true }
function exitB(){ state.b = false }
然后,如果您有 jQuery 1.4.2,则可以绑定(bind)多个事件,因此请在第一个事件之后添加这些事件。
$('.a,.b').mouseout(function(){
//we're outside of both blocks
if( !state.a && !state.b )
{
//do something
}
});
当您离开 A 或 B 并且您也在另一个之外时,此操作将会触发。我想这就是你的意思。
关于jquery - 这个在 jQuery 里怎么说?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3723032/