这个怎么说在jQuery?

Lil*_*ool 1 jquery

如果用户的鼠标已经离开了两个html元素(文本字段和跨区块),我想提醒一些事情.我如何在jquery中说AND,徒劳无功我试过这样的事情:

if ($('textarea.commentField') && $('span.loginPrompt')).mouseout(function() {
     alert('something');
});
Run Code Online (Sandbox Code Playgroud)

Ste*_*all 7

你原则上不能这样做,因为这需要一些腿部工作.

这样的东西可行,但必须有一个更好的解决方案:

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 }
Run Code Online (Sandbox Code Playgroud)

然后,如果你有jQuery 1.4.2,你可以绑定多个事件,所以在第一个事件之后添加它们.

$('.a,.b').mouseout(function(){ 
//we're outside of both blocks
if( !state.a && !state.b )
{
   //do something
}
});
Run Code Online (Sandbox Code Playgroud)

当你离开A或B时会发射,而你也在另一个之外.我想这就是你的意思.

  • 但是必须有更好的方法.理智决定了这样的:P. (3认同)