标题显示当我在按钮的下拉菜单外点击时,我的脚本不想工作
$(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>
&#13;
当我点击登录按钮时,它会变为活动课程,这会更改按钮的背景,这很好但是当我点击外面时它不想工作,我希望活动课程能够被删除,css颜色返回。我做错了什么?
答案 0 :(得分:2)
您的mouseup
事件只是添加:
.removeClass("active");
到
$(".openmenu").attr('id', '');
像这样:
$(document).mouseup(function(){
$(".submenu").hide();
$(".openmenu").attr('id', '').removeClass("active"); <-----
});
将来,请不要将jquery库复制到小提琴中,只需从侧面菜单中选择jquery