我添加了代码来创建一个div,#pending-friend-list-dropdown
,点击它之外时关闭。这工作正常,但现在当点击我的图像股利,friend-icon
,下拉div现在不会关闭。当点击它时隐藏一个div
正如你可以在我的代码片段中看到的,图像div是打开下拉框的内容。我只是想弄清楚如何使用图像div来打开和关闭下拉菜单,同时使用mouseup函数关闭下拉div。
//Hiding Pending Friend Drop-down when clicking out of it
$(document).mouseup(function (e)
{
var container = $("#pending-friend-list-dropdown");
\t var friend_icon = $("#friend-icon");
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();
}
\t else if (friend_icon.has(e.target)) {
\t \t container.hide();
\t }
});
//Toggle Pending Friend List
$("#friend-icon").click(function() {
\t $('#pending-friend-list-dropdown').toggle(100);
});
#main-bar {
\t width: 85%;
\t height: 60px;
\t position: relative;
\t margin-left: 15%;
\t background: red;
\t padding: 3px 0;
}
#main-bar-container {
\t border: 1px solid black;
\t margin: 0 10px;
\t position: relative;
\t width: 95%;
\t height: 56px;
\t left: 2%;
}
/*---- Pending Friends List----*/
#friend-icon {
\t display: inline-block;
\t cursor: pointer;
\t position: absolute;
\t right: 20%;
\t top: 15px;
}
#friend-icon img {
\t height: 30px;
\t width: 30px;
}
#pending-friend-list-dropdown {
\t height: 500px;
\t width: 400px;
\t overflow: scroll;
\t z-index: 100000;
\t position: absolute;
\t left: 70%;
\t top: 70px;
\t background: blue;
\t display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main-bar">
\t \t <div id="main-bar-container">
\t \t \t <div id="friend-icon"><img src="../icons/collection/social.png" alt="Pending Friends"></div>
\t \t </div>
\t </div>
<div id="pending-friend-list-dropdown">
</div>
完美。谢谢! – Paul
请注意,如果用户点击其中有onclick定义的元素,这将无法正常工作。然后,这个onclick将被激活,而不是你的根级。只是要留意一下。 – k2snowman69