Jquery 单击外部时无法关闭下拉列表
当我在按钮外单击时,无法关闭下拉列表上面的函数会使下拉按钮完全工作。页面底部的函数是我试图开始工作的时候,它使得脚本中的另一个函数无法使用,如果我删除它,工作就会停止。下面代码中的dropwdown html只是一个示例Jquery 单击外部时无法关闭下拉列表,jquery,html,css,Jquery,Html,Css,当我在按钮外单击时,无法关闭下拉列表上面的函数会使下拉按钮完全工作。页面底部的函数是我试图开始工作的时候,它使得脚本中的另一个函数无法使用,如果我删除它,工作就会停止。下面代码中的dropwdown html只是一个示例 //this is how the dropdown looks like got it from //http://www.w3schools.com/w3css/w3css_dropdowns.asp <div class="w3-d
//this is how the dropdown looks like got it from //http://www.w3schools.com/w3css/w3css_dropdowns.asp
<div class="w3-dropdown-click">
<button onclick="myFunction()" class="w3-btn">ClickMe</button>
<div id="Demo" class="w3-dropdown-content w3-card">
/*some example links*/
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
// I use two of these on my page
<script>
//this functions open and closes my dropdown when clicked
function myFunction(event) {
//opens the selected dropdownlist
document.getElementById("Demo").classList.toggle("w3-show");
//closes the other dropdownlist
document.getElementById("Demo2").classList.remove("w3-show");
event.stopPropagation();
}
//this functions open and closes my other dropdown when clicked
function myFunction2(event) {
//opens the selected dropdownlist
document.getElementById("Demo2").classList.toggle("w3-show");
//closes the other dropdownlist
document.getElementById("Demo").classList.remove("w3-show");
event.stopPropagation();
}
// the funtion im trying to get to work it should close the dropdown when Im clicking outsid of the dropdown buttons
$(":not(#Demo2)").click( function(){
document.getElementById("Demo").classList.remove("w3-show");
document.getElementById("Demo2").classList.remove("w3-show");
});
</script>
让我们试试这个方法,好吗 构造HTML以执行客户端的点击平衡。使用IDs对存储过程进行编程 HTML
JQUERY让我们试试这种方法,好吗 构造HTML以执行客户端的点击平衡。使用IDs对存储过程进行编程 HTML JQUERY window.addEventListener'mouseup',functionevent{ var pol=document.getElementById'Demo1'; ifevent.target!=pol&&event.target.parentNode!=pol{ document.getElementByIdDemo.classList.removew3-show; document.getElementByIdDemo2.classList.removew3-show; } }; window.addEventListener'mouseup',functionevent{ var pol=document.getElementById'Demo1'; ifevent.target!=pol&&event.target.parentNode!=pol{ document.getElementByIdDemo.classList.removew3-show; document.getElementByIdDemo2.classList.removew3-show; }
}; 如上所述,这可能会对您有所帮助,您可以添加一个click listener并检查列表是否被单击,如果没有,您可以切换菜单。我认为问题在于,我不知道在//event.stopPropagation;在这种情况下没关系我想这可能会对你有所帮助如上所述,你可以添加一个点击监听器,检查你的列表是否被点击,如果没有,你可以切换你的菜单。我想问题是我不知道我应该从//event.stopPropagation;我想在这种情况下没关系
<div class="top40">
<button type="button" class="btn" data-toggle="collapse" data-target="#demo">
Collapse Button
</button>
<ul id="demo" class="collapse">
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
</ul>
</div>
.top40{
margin-top:40px;
}
.btn {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
color: #fff;
background-color: #337ab7;
border-color: #2e6da4;
}
.collapse {
display: none;
}
.collapse.in {
display: block;
}
$(document).on('click',function(){
$('.collapse').toggleClass('in');
})