none - jquery toggle show hide example
當用戶點擊它時,使用jQuery隱藏DIV (20)
我正在使用此代碼:
$('body').click(function() {
$('.form_wrapper').hide();
});
$('.form_wrapper').click(function(event){
event.stopPropagation();
});
而這個HTML:
<div class="form_wrapper">
<a class="agree" href="javascript:;">I Agree</a>
<a class="disagree" href="javascript:;">Disagree</a>
</div>
問題是我在DIV內部有鏈接,點擊時他們不再工作。
prc322的真棒答案。
function hideContainerOnMouseClickOut(selector, callback) {
var args = Array.prototype.slice.call(arguments); // Save/convert arguments to array since we won't be able to access these within .on()
$(document).on("mouseup.clickOFF touchend.clickOFF", function (e) {
var container = $(selector);
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
$(document).off("mouseup.clickOFF touchend.clickOFF");
if (callback) callback.apply(this, args);
}
});
}
這增加了一些東西......
- 放置在帶有“無限制”參數的回調函數中
- 添加了一個調用jquery的.off()與事件命名空間配對,以便在文檔運行後從文檔中解除綁定事件。
- 包括移動功能的touchend
我希望這可以幫助別人!
(只需加入prc322的答案。)
在我的情況下,我使用這段代碼隱藏了當用戶點擊一個適當的選項卡時出現的導航菜單。 我發現添加一個額外的條件非常有用,即容器外點擊的目標不是鏈接。
$(document).mouseup(function (e)
{
var container = $("YOUR CONTAINER SELECTOR");
if (!$("a").is(e.target) // if the target of the click isn't a link ...
&& !container.is(e.target) // ... or the container ...
&& container.has(e.target).length === 0) // ... or a descendant of the container
{
container.hide();
}
});
這是因為我網站上的某些鏈接向頁面添加了新內容。 如果在導航菜單消失的同時添加新內容,則可能會導致用戶迷失方向。
適用於桌面和移動設備
var notH = 1,
$pop = $('.form_wrapper').hover(function(){ notH^=1; });
$(document).on('mousedown keydown', function( e ){
if(notH||e.which==27) $pop.hide();
});
如果在某些情況下,您需要確保在點擊文檔時確實顯示了您的元素: if($pop.is(':visible') && (notH||e.which==27)) $pop.hide();
檢查點擊區域不在目標元素或其子項中
$(document).click(function (e) {
if ($(e.target).parents(".dropdown").length === 0) {
$(".dropdown").hide();
}
});
更新:
jQuery停止傳播是最好的解決方案
$(".button").click(function(e){
$(".dropdown").show();
e.stopPropagation();
});
$(".dropdown").click(function(e){
e.stopPropagation();
});
$(document).click(function(){
$(".dropdown").hide();
});
你可能想檢查點擊事件的目標,而不是依靠stopPropagation。
就像是:
$("body").click
(
function(e)
{
if(e.target.className !== "form_wrapper")
{
$(".form_wrapper").hide();
}
}
);
而且,body元素可能不包含瀏覽器中顯示的整個可視空間。 如果您注意到您的點擊未註冊,則可能需要添加HTML元素的點擊處理程序。
你最好去這樣的事情:
var mouse_is_inside = false;
$(document).ready(function()
{
$('.form_content').hover(function(){
mouse_is_inside=true;
}, function(){
mouse_is_inside=false;
});
$("body").mouseup(function(){
if(! mouse_is_inside) $('.form_wrapper').hide();
});
});
如果你在使用ios時遇到問題,mouseup在蘋果設備上不起作用。
在jQuery中為ipad做了mousedown / mouseup嗎?
我使用這個:
$(document).bind('touchend', function(e) {
var container = $("YOURCONTAINER");
if (container.has(e.target).length === 0)
{
container.hide();
}
});
如果您點擊.form_wrapper,則返回false:
$('body').click(function() {
$('.form_wrapper').click(function(){
return false
});
$('.form_wrapper').hide();
});
//$('.form_wrapper').click(function(event){
// event.stopPropagation();
//});
將解決方案更新為:
- 改用mouseenter和mouseleave
- 懸停使用實時事件綁定
var mouseOverActiveElement = false;
$('.active').live('mouseenter', function(){
mouseOverActiveElement = true;
}).live('mouseleave', function(){
mouseOverActiveElement = false;
});
$("html").click(function(){
if (!mouseOverActiveElement) {
console.log('clicked outside active element');
}
});
對於像IPAD和IPHONE這樣的觸控設備,我們可以使用以下代碼
$(document).on('touchstart', function (event) {
var container = $("YOUR CONTAINER SELECTOR");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});
我想要最喜歡的建議,但它不適合我。
這種方法幾乎相同,但為我工作。 http://www.codesynthesis.co.uk/code-snippets/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of-it
我是這樣做的:
var close = true;
$(function () {
$('body').click (function(){
if(close){
div.hide();
}
close = true;
})
alleswasdenlayeronclicknichtschliessensoll.click( function () {
close = false;
});
});
會不會有這樣的工作?
$("body *").not(".form_wrapper").click(function() {
});
要么
$("body *:not(.form_wrapper)").click(function() {
});
有同樣的問題,提出了這個簡單的解決方案。 它甚至工作遞歸:
$(document).mouseup(function(e)
{
var container = $("YOUR CONTAINER SELECTOR");
// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0)
{
container.hide();
}
});
這是我在另一個線程上找到的jsfiddle,也可以使用esc鍵: http://jsfiddle.net/S5ftb/404 : http://jsfiddle.net/S5ftb/404
var button = $('#open')[0]
var el = $('#test')[0]
$(button).on('click', function(e) {
$(el).show()
e.stopPropagation()
})
$(document).on('click', function(e) {
if ($(e.target).closest(el).length === 0) {
$(el).hide()
}
})
$(document).on('keydown', function(e) {
if (e.keyCode === 27) {
$(el).hide()
}
})
通過使用此代碼,您可以隱藏盡可能多的項目
var boxArray = ["first element's id","second element's id","nth element's id"];
window.addEventListener('mouseup', function(event){
for(var i=0; i < boxArray.length; i++){
var box = document.getElementById(boxArray[i]);
if(event.target != box && event.target.parentNode != box){
box.style.display = 'none';
}
}
})
var exclude_div = $("#ExcludedDiv");;
$(document).click(function(e){
if( !exclude_div.is( e.target ) ) // if target div is not the one you want to exclude then add the class hidden
$(".myDiv1").addClass("hidden");
});
$('body').click(function(event) {
if (!$(event.target).is('p'))
{
$("#e2ma-menu").hide();
}
});
p
是元素名稱。 哪裡可以傳遞id或類或元素名稱。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<p>If you click on the "Hide" button, I will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
dojo.query(document.body).connect('mouseup',function (e)
{
var obj = dojo.position(dojo.query('div#divselector')[0]);
if (!((e.clientX > obj.x && e.clientX <(obj.x+obj.w)) && (e.clientY > obj.y && e.clientY <(obj.y+obj.h))) ){
MyDive.Hide(id);
}
});