2016-10-26 4 views
-1

私は、ボタンからドロップダウンメニューを作ることについてW3Schoolsのウェブサイトのtutorialに続きました。ユーザーがドロップダウンメニューの中をクリックすると、メニュー自体が消えない限り、すべてうまく動作します。ユーザーがメニューをクリックすると、ドロップダウンメニューが閉じないようにする方法

ユーザーがボタンをクリックする場所にLOGINボタンを作成したかったので、ユーザー名とパスワードを入力できるドロップダウンメニューを表示し、ログインをクリックします。

ユーザーが他の領域をクリックすると外のドロップダウンメニューがドロップダウンメニューを自動的に閉じる場所を保持したいと思います。

私の問題は、ユーザがの中のいずれかの領域をクリックするとドロップダウンメニューが消えることです。です。 JavaScriptのみを使用してドロップダウンメニュー内をクリックしているときに、その表示が消えないようにするにはどうすればよいですか?何window.onClickハンドラでjQueryの(私はまだjQueryを使って実際に慣れていないよ)

/* When the user clicks on the button, 
 
toggle between hiding and showing the dropdown content */ 
 
function myFunction() { 
 
    document.getElementById("myDropdown").classList.toggle("show"); 
 
} 
 

 
// Close the dropdown menu if the user clicks outside of it 
 
window.onclick = function(event) { 
 
    if (!event.target.matches('.dropbtn')) { 
 

 
    var dropdowns = document.getElementsByClassName("dropdown-content"); 
 
    var i; 
 
    for (i = 0; i < dropdowns.length; i++) { 
 
     var openDropdown = dropdowns[i]; 
 
     if (openDropdown.classList.contains('show')) { 
 
     openDropdown.classList.remove('show'); 
 
     } 
 
    } 
 
    } 
 
}
/* Dropdown Button */ 
 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
/* Dropdown button on hover & focus */ 
 
.dropbtn:hover, .dropbtn:focus { 
 
    background-color: #3e8e41; 
 
} 
 

 
/* The container <div> - needed to position the dropdown content */ 
 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
/* Dropdown Content (Hidden by Default) */ 
 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
/* Contents inside the dropdown */ 
 
.dropdown-content input, .dropdown-content button { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
/* Change color of dropdown links on hover */ 
 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ 
 
.show {display:block;}
<div class="dropdown"> 
 
    <button onclick="myFunction()" class="dropbtn">LOG IN</button> 
 
    <div id="myDropdown" class="dropdown-content"> 
 
    <input type="text" name="username" placeholder="Enter username"/> 
 
    <input type="text" name="password" placeholder="Enter password"/> 
 
    <button type="submit" name="submit">Submit</button> 
 
    </div> 
 
</div>

+0

ショーのクラスを削除しないでください、あなたは、ページ上の唯一のドロップダウンを持っているつもりですか? –

+0

異なるクラス名とIDを持つ

+0

内部でクリックすると閉じられるはずですか? –

答えて

1

を使用する方法について、私たちは、「ドロップダウンコンテンツ」とその子を除外するための条件を追加します。ドロップダウン、 内をクリックする

/* When the user clicks on the button, 
 
toggle between hiding and showing the dropdown content */ 
 
function myFunction() { 
 
    document.getElementById("myDropdown").classList.toggle("show"); 
 
} 
 

 
// Close the dropdown menu if the user clicks outside of it 
 
window.onclick = function(event) { 
 
    if (!event.target.matches('.dropbtn')) { 
 
    // [BEGIN][Here is the condition] 
 
    if (event.target.matches('.dropdown-content') || event.target.matches('.dropdown-content *')) { 
 
     event.stopPropagation(); 
 
     return; 
 
    } 
 
    // [END][Here is the condition] 
 
    var dropdowns = document.getElementsByClassName("dropdown-content"); 
 
    var i; 
 
    for (i = 0; i < dropdowns.length; i++) { 
 
     var openDropdown = dropdowns[i]; 
 
     if (openDropdown.classList.contains('show')) { 
 
     openDropdown.classList.remove('show'); 
 
     } 
 
    } 
 
    } 
 
}
/* Dropdown Button */ 
 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
/* Dropdown button on hover & focus */ 
 
.dropbtn:hover, .dropbtn:focus { 
 
    background-color: #3e8e41; 
 
} 
 

 
/* The container <div> - needed to position the dropdown content */ 
 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
/* Dropdown Content (Hidden by Default) */ 
 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
/* Contents inside the dropdown */ 
 
.dropdown-content input, .dropdown-content button { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
/* Change color of dropdown links on hover */ 
 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ 
 
.show {display:block;}
<div class="dropdown"> 
 
    <button onclick="myFunction()" class="dropbtn">LOG IN</button> 
 
    <div id="myDropdown" class="dropdown-content"> 
 
    <input type="text" name="username" placeholder="Enter username"/> 
 
    <input type="text" name="password" placeholder="Enter password"/> 
 
    <button type="submit" name="submit">Submit</button> 
 
    </div> 
 
</div>

0

あなたは

/* When the user clicks on the button, 
 
toggle between hiding and showing the dropdown content */ 
 
function myFunction() { 
 
    document.getElementById("myDropdown").classList.toggle("show"); 
 
} 
 

 
// Close the dropdown menu if the user clicks outside of it 
 
window.onclick = function(event) { 
 
    
 
    if (event.target.matches('.dropdown-content *')) { 
 
    return; 
 
    } 
 

 
    if (!event.target.matches('.dropbtn')) { 
 

 
    var dropdowns = document.getElementsByClassName("dropdown-content"); 
 
    var i; 
 
    for (i = 0; i < dropdowns.length; i++) { 
 
     var openDropdown = dropdowns[i]; 
 
     if (openDropdown.classList.contains('show')) { 
 
     openDropdown.classList.remove('show'); 
 
     } 
 
    } 
 
    } 
 
}
/* Dropdown Button */ 
 
.dropbtn { 
 
    background-color: #4CAF50; 
 
    color: white; 
 
    padding: 16px; 
 
    font-size: 16px; 
 
    border: none; 
 
    cursor: pointer; 
 
} 
 

 
/* Dropdown button on hover & focus */ 
 
.dropbtn:hover, .dropbtn:focus { 
 
    background-color: #3e8e41; 
 
} 
 

 
/* The container <div> - needed to position the dropdown content */ 
 
.dropdown { 
 
    position: relative; 
 
    display: inline-block; 
 
} 
 

 
/* Dropdown Content (Hidden by Default) */ 
 
.dropdown-content { 
 
    display: none; 
 
    position: absolute; 
 
    background-color: #f9f9f9; 
 
    min-width: 160px; 
 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
 
} 
 

 
/* Contents inside the dropdown */ 
 
.dropdown-content input, .dropdown-content button { 
 
    color: black; 
 
    padding: 12px 16px; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 

 
/* Change color of dropdown links on hover */ 
 
.dropdown-content a:hover {background-color: #f1f1f1} 
 

 
/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ 
 
.show {display:block;}
<div class="dropdown"> 
 
    <button onclick="myFunction()" class="dropbtn">LOG IN</button> 
 
    <div id="myDropdown" class="dropdown-content"> 
 
    <input type="text" class="form-input" name="username" placeholder="Enter username"/> 
 
    <input type="text" class="form-input" name="password" placeholder="Enter password"/> 
 
    <button type="submit" name="submit">Submit</button> 
 
    </div> 
 
</div>

関連する問題