2015-10-04 163 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