javascript 下拉菜单在点击时激活,在外部点击时不激活

标签 javascript jquery html css

正如标题所说,当我在登录按钮的下拉菜单外单击时,我的脚本不想工作

jsfiddle Demo

$(document).ready(function() {
  $(".openmenu").click(function() {
    var X = $(this).attr('id');
    if (X == 1) {
      $(".submenu").hide();
      $(this).attr('id', '0');
    } else {
      $(".submenu").show();
      $(this).attr('id', '1');
    }
  });
  $(".submenu").mouseup(function() {
    return false
  });
  $(".openmenu").mouseup(function() {
    return false
  });
  $(document).mouseup(function() {
    $(".submenu").hide();
    $(".openmenu").attr('id', '');
  });
});

$(document).ready(function() {
  myInit()
})


$(document).ready(function() {
  myInit()
})

function myInit() {
  $('.openmenu').click(function() {
    $('.openmenu').removeClass('active')
    $(this).addClass('active')
  })
}
/* RESET CSS */

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}
ol,
ul {
  list-style: none;
}
blockquote,
q {
  quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
/* BODY */

body {
  font-family: "Myraid Pro", Arial;
  overflow-y: scroll;
  overflow-x: hidden;
  color: #333;
  font-size: 14px;
}
.clear {
  clear: both;
}
/* FLOATS */

.left {
  float: left;
}
.right {
  float: right;
}
#top-bar {
  background: #619B27;
}
#top-bar-inner {
  margin: auto;
  width: 500px;
  font-size: 14px;
}
#top-bar {
  background: #619B27;
}
#top-bar-inner {
  margin: auto;
  width: 300px;
  font-size: 14px;
  position: relative;
}
#top-bar li {
  display: inline-block;
  margin-left: 10px;
}
#top-bar a {
  display: block;
  padding: 10px;
  color: #FFF;
  text-decoration: none;
  outline: 0;
}
#top-bar a:hover {
  background: #4d7c1f;
}
.active {
  background: #CC0;
}
.submenu {
  background: #CC0;
  position: absolute;
  right: 0;
  width: 200px;
  height: 250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="top-bar">
  <div id="top-bar-inner">
    <div class="right">
      <ul>
        <li><a href="/">Register</a>
        </li>
        <li><a href="#" class="openmenu">Sign In</a>
        </li>
        <div class="submenu" style="display: none;">
          <p>test</p>
        </div>
      </ul>
    </div>
    <div class="clear"></div>
  </div>
</div>

当我单击“登录”按钮时,它会更改为事件类,这会更改按钮的背景,这很好,但是当我在外部单击时它不想工作,我希望删除事件类和 css 颜色返回。我做错了什么?

最佳答案

您的 mouseup 事件只需添加:

.removeClass("active");

$(".openmenu").attr('id', '');

像这样:

$(document).mouseup(function(){
   $(".submenu").hide();
   $(".openmenu").attr('id', '').removeClass("active"); <-----
});

FIDDLE

以后,请不要将 jquery 库复制到 fiddle 中,只需从侧面菜单中选择 jquery

关于javascript 下拉菜单在点击时激活,在外部点击时不激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27727202/

相关文章:

javascript - iOS 平台的 React Native 中加密图像并将其保存在 Gallery 中的方法?

javascript - 我可以使用哪些方法来重新排列 javascript 函数的执行顺序?

javascript - 保护图像免遭从网站下载

javascript - Jasmine 无法检测复选框

html - 软件用户帮助文档(来自应用程序的 chm、html 和超链接)

javascript - 在收到特定操作时将商店重置为初始状态

javascript - 使 div 上的颜色根据您单击的位置而变化

javascript - 如何仅使用 jquery .slidetoggle 显示/隐藏单个 div

javascript - 如何在单击时淡出 1 张图片并自动淡入彼此靠近的 4 张图片?

javascript - 如何在不调整主要内容边距的情况下设置可折叠的侧边导航?