2015-10-04 162 views
6

假设禁用绑定我有这样的HTML:我怎样才能在一个div

<body> 
    <div id="topbar"> 
     First block here. 
     <p>Another block here.</p> 
    </div> 
    <div class="header"> 
     <div class="container"></div> 
    </div> 
    <div id="footer">Footer</div> 
</body> 

如何禁用对div#topbarbind

$('body').bind('mouseover mouseout', function(event) { }); 

我已经尝试这个没有成功:

if($(event.target).is('#topbar')) { //do nothing } 
else { //do stuff } 

其实,我的问题是,如果我做的p标签的鼠标悬停,代码不工作。

PS:我对HTML没有任何控制权,所以我必须使用通用标记,如body

谢谢。

回答

1

尝试创建另一个DIV对身体的其余部分:

<body> 
    <div id="topbar">hello 
     <div>How<br>are<br>you<br>today<br>?</div> 
    </div> 
    <div id="mouseEvent"> 
     <div class="header"> 
      <div class="container"></div> 
     </div> 
     <div id="footer">Footer</div> 
    </div> 
</body> 

和JS:

$('body').bind('mouseover mouseout', function(event) { 
    var list = $('#topbar').find("*"); 
    if($(event.target).is("#topbar") || $(event.target).is(list)) { 
     alert("hello"); 
    } else { 
     alert("footer"); 
    } 
}); 

更新时间: JSFiddle

+0

由于我不知道,如果#mouseEvent网站上存在我无法使用它。我必须使用像身体一样的通用标签。 – PacPac

+0

@PacPac好的一会儿 – morha13

+0

@PacPac已更新回答 – morha13