ページ上の任意の場所をクリックしたときに表示される要素を非表示にするこれが正しい方法であるかどうかを知りたいです。
$(document).click(function (event) {
$('#myDIV:visible').hide();
});
要素 (div、span など) は、要素の境界内でクリック イベントが発生したときに消えてはなりません。
ページ上の任意の場所をクリックしたときに表示される要素を非表示にするこれが正しい方法であるかどうかを知りたいです。
$(document).click(function (event) {
$('#myDIV:visible').hide();
});
要素 (div、span など) は、要素の境界内でクリック イベントが発生したときに消えてはなりません。
私が理解している場合は、div 以外の場所をクリックしたときに div を非表示にする必要があります。次のコードでそれを行うことができます。
$(document).click(function() {
alert("me");
});
$(".myDiv").click(function(e) {
e.stopPropagation(); // This is the preferred method.
return false; // This should not be used unless you do not want
// any click events registering inside the div
});
これにより、クリックがページ全体にバインドされますが、問題の div をクリックすると、クリック イベントがキャンセルされます。
これを試して
$('.myDiv').click(function(e) { //button click class name is myDiv
e.stopPropagation();
})
$(function(){
$(document).click(function(){
$('.myDiv').hide(); //hide the button
});
});
私は ID の代わりにクラス名を使用します。
編集 -別のピースを追加したため、次のように機能します。
$('.myDiv').click(function() { //button click class name is myDiv
e.stopPropagation();
})
$(function(){
$('.openDiv').click(function() {
$('.myDiv').show();
});
$(document).click(function(){
$('.myDiv').hide(); //hide the button
});
});
jQuery 1.7 の時点で、イベントを処理する新しい方法があります。これを「新しい」方法で行う方法を示すために、ここで答えると思いました。まだお持ちでない場合は、「on」メソッドの jQuery ドキュメントを読むことをお勧めします。
var handler = function(event){
// if the target is a descendent of container do nothing
if($(event.target).is(".container, .container *")) return;
// remove event handler from document
$(document).off("click", handler);
// dostuff
}
$(document).on("click", handler);
ここでは、jQuery のセレクターとイベントのバブリングを悪用しています。後でイベント ハンドラーをクリーンアップするように注意してください。この動作は$('.container').one
( docs を参照) で自動化できますが、ハンドラー内で条件を実行する必要があるため、ここでは適用できません。
この次のコード例は、私にとって最もうまくいくようです。divまたはその子のイベントのすべての処理を停止する「return false」を使用できますが。ポップアップ div (ポップアップ ログイン フォームなど) を制御したい場合は、event.stopPropogation() を使用する必要があります。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a id="link" href="#">show box</a>
<div id="box" style="background: #eee; display: none">
<p>a paragraph of text</p>
<input type="file" />
</div>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var box = $('#box');
var link = $('#link');
link.click(function() {
box.show(); return false;
});
$(document).click(function() {
box.hide();
});
box.click(function(e) {
e.stopPropagation();
});
</script>
</body>
</html>
ありがとう、トーマス。私はJSが初めてで、問題の解決策を探していました。あなたのことが役に立ちました。
jquery を使用して、下にスライドするログイン ボックスを作成しました。最高のユーザー エクスペリエンスを実現するために、ユーザーがボックス以外の場所をクリックするとボックスが消えるようにしました。これを修正するのに約 4 時間を費やすのは少し恥ずかしいです。でもねえ、私はJSが初めてです。
多分私のコードは誰かを助けることができます:
<body>
<button class="login">Logg inn</button>
<script type="text/javascript">
$("button.login").click(function () {
if ($("div#box:first").is(":hidden")) {
$("div#box").slideDown("slow");}
else {
$("div#box").slideUp("slow");
}
});
</script>
<div id="box">Lots of login content</div>
<script type="text/javascript">
var box = $('#box');
var login = $('.login');
login.click(function() {
box.show(); return false;
});
$(document).click(function() {
box.hide();
});
box.click(function(e) {
e.stopPropagation();
});
</script>
</body>
私は以下を行いました。他の誰かが利益を得ることができるように共有することを考えました。
$("div#newButtonDiv").click(function(){
$(this).find("ul.sub-menu").css({"display":"block"});
$(this).click(function(event){
event.stopPropagation();
$("html").click(function(){
$(this).find("ul.sub-menu").css({"display":"none"});
}
});
});
これについて誰かを助けることができるかどうか教えてください。
あなたもできることは次のとおりです。
$(document).click(function (e)
{
var container = $("div");
if (!container.is(e.target) && container.has(e.target).length === 0)
{
container.fadeOut('slow');
}
});
ターゲットが div でない場合は、その長さがゼロに等しいことを確認して div を非表示にします。
これを試して:
$(document).mouseup(function (e) {
var div = $("#yourdivid");
if (!div.is(e.target) && div.has(e.target).length === 0)
{
div.hide();
}
});
$(document).on('click', function(e) { // Hides the div by clicking any where in the screen
if ( $(e.target).closest('#suggest_input').length ) {
$(".suggest_div").show();
}else if ( ! $(e.target).closest('.suggest_container').length ) {
$('.suggest_div').hide();
}
});
ここで #suggest_input in はテキストボックスの名前、.suggest_container は ul クラス名、.suggest_div は自動提案のメイン div 要素です。
このコードは、画面内の任意の場所をクリックして div 要素を非表示にするためのものです。すべてを行う前に、コードを理解してコピーしてください...
非子要素でクリックが発生したときにコンテナー div を非表示にする別の方法。
$(document).on('click', function(e) {
if(!$.contains($('.yourContainer').get(0), e.target)) {
$('.yourContainer').hide();
}
});
これは、Sandeep Pal の回答に基づいた有効な CSS/小さな JS ソリューションです。
$(document).click(function (e)
{
if (!$("#noticeMenu").is(e.target) && $("#noticeMenu").has(e.target).length == 0)
{
$("#menu-toggle3").prop('checked', false);
}
});
チェックボックスをクリックしてから、メニューの外側をクリックして試してください。
$('html').click(function() {
//Hide the menus if it is visible
});
$('#menu_container').click(function(event){
event.stopPropagation();
});
しかし、これらのことにも留意する必要があります。http://css-tricks.com/dangers-stopping-event-propagation/
これは機能しません。内部をクリックすると、.myDIVが非表示になります。
$('.openDiv').click(function(e) {
$('.myDiv').show();
e.stopPropagation();
})
$(document).click(function(){
$('.myDiv').hide();
});
});
<a class="openDiv">DISPLAY DIV</a>
<div class="myDiv">HIDE DIV</div>
上記の提案に対する2つの小さな改善:
クリックを想定して、これをトリガーしたイベントの伝播を停止します
jQuery(thelink).click(function(e){
jQuery(thepop).show();
// bind the hide controls
var jpop=jQuery(thepop);
jQuery(document).bind("click.hidethepop", function() {
jpop.hide();
// unbind the hide controls
jQuery(document).unbind("click.hidethepop");
});
// dont close thepop when you click on thepop
jQuery(thepop).click(function(e) {
e.stopPropagation();
});
// and dont close thepop now
e.stopPropagation();
});
$(document).ready(function(){
$("button").click(function(){
$(".div3").toggle(1000);
});
$("body").click(function(event) {
if($(event.target).attr('class') != '1' && $(event.target).attr('class') != 'div3'){
$(".div3").hide(1000);}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<button class="1">Click to fade in boxes</button><br><br>
<body style="width:100%;height:200px;background-color:pink;">
<div class="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div><body>
これは、上記の他のソリューションから作成されます。
$(document).ready(function(){
$("button").click(function(event){
$(".area").toggle();
event.stopPropagation(); //stops the click event to go "throu" the button an hit the document
});
$(document).click(function() {
$(".area").hide();
});
$(".interface").click(function(event) {
event.stopPropagation();
return false;
});
});
<div>
<div>
<button> Press here for content</button>
<div class="area">
<div class="interface"> Content</div>
</div>
</div>
</div>