2014-04-24 49 views
0

因此,當用戶點擊到的div珀普它觸發隱藏(),這是很好的,但是當我點擊文本,它接近,但我希望它繼續開放。jQuery的點擊進入格觸發,孩子的div觸發它

HTML:

<div id="s" class="popu"><img class="tri" src="img/whtri.png"/> 
    <div class="textb"><center style="font-size:14px;">Item Title</center> 
     <span style="font-size:12px;">Description this is an item that is very good and i like it very much! I like <a href="f.php">More...</a> 
     </span> 
     <span style=""> 
     </span> 
    </div> 
</div> 

JAVASCRIPT/JQUERY:

$("body").click(function (e) { 
     if (!$(e.target).hasClass("popu")) { 
      $("#s").hide(200) 
     } 
    }) 

請記住,這是代碼

片斷任何幫助將真棒!

+0

*點擊到DIV珀普*你怎麼能這樣做呢?它沒有任何意義,div只是一個容器,它包含一個圖像,一個鏈接,一些文本,所以你點擊了什麼? –

+0

所以,你想要什麼是當點擊'popu' div'#s'被隱藏,但是當點擊'popu' div內的任何東西時,#s'不會被隱藏? –

+0

@DerekS是的,就是它 – Eric

回答

1

不要只檢查是否有點擊的元素的類,它會點擊元素的任何孩子失敗時,檢查是否任何父或點擊的元素有類,它可以與closest()

完成
$("body").click(function (e) { 
    if (!$(e.target).closest(".popu").length) { 
     $("#s").hide(200) 
    } 
}); 
0

我會去這個 -

$(".tri").click(function (e) { 
    if($(this).parent().hasClass('popu')){ 
     $("#s").hide(200) 
    } 
}) 

試驗圖像上的點擊,發現如果家長有正確的類,然後隱藏如果合適的話。

0

如果你想#s僅在珀普DIV,當點擊,但珀普專區內點擊任何東西時隱藏,#秒不躲。

嘗試刪除not!

$("body").click(function (e) { 
     if ($(e.target).hasClass("popu")) { 
      $("#s").hide(200) 
     } 
    }) 

Working Fiddle