如果兄弟姐妹没有":活跃"执行功能

时间:2014-08-16 06:49:23

标签: javascript jquery html

如果对象兄弟没有伪类":active"如何执行函数?使用jQuery?这个问题的原因是我希望在光标悬停在某个对象上时执行该函数,并且其兄弟的NONE是":active" (基本含义:我希望它在鼠标悬停在对象上时执行,而不是在任何兄弟上单击)。

获得想法:

HTML:

<div class="tobehovered">...</div>
<div class="sibling">...</div>
...

JQuery的:

function theOneToBeExecutedOnHover(){...}
$(".tobehovered").mouseenter(function(){
  if( ... /* calculation???-ish */) theOneToBeExecutedOnHover();
}

jQuery不必完全按照这种方式进行格式化。我的例子只是为了原则。

2 个答案:

答案 0 :(得分:2)

从您的悬停元素$(this)中,找到兄弟姐妹.siblings(),过滤活跃的.filter(':active'),然后检查您是否还没有.length === 0

可以这样缩短:

if ($(this).siblings(':active').length === 0) {

答案 1 :(得分:1)

试试这个

$(".tobehovered").mouseenter(function(event){
  if(event.target.className == 'active'){
     return;
  }

  //here execute function

}