JavaScript 下拉菜单在单击时处于活动状态,在外部单击时不活动

javascript drop menu active on click and not active when click outside

本文关键字:单击 外部 不活动 活动状态 下拉菜单 JavaScript      更新时间:2023-09-26

标题显示我的脚本不想工作,当我单击按钮登录的下拉菜单外

杰斯菲德尔演示

$(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"); <-----
});

小提琴

以后请不要将jquery库复制到小提琴中,只需从侧边菜单中选择jquery即可。