j下拉菜单的查询问题
jQuery issue with drop down menu
问题是:
- 下拉
menu(#sub-button)
通过鼠标单击关闭,仅当单击可见#button
时,它才应关闭 - 例如,当我尝试打开第二个下拉菜单时
menu(#sub-button2)
单击第一个下拉菜单时应立即关闭#button2
网页代码:
<html>
<head>
<title>jquery ddm</title>
<style type="text/css">
#button {
position:fixed;
width:150px;
height:40px;
background-color:blue;
}
#sub-button {
display: none;
width:150px;
height:30px;
margin-top:41px;
background-color:cyan;
}
#button2 {
position:fixed;
width:150px;
height:40px;
background-color:orange;
}
#sub-button2 {
display: none;
width:150px;
height:30px;
margin-top:41px;
background-color:aqua;
}
</style>
</head>
<body>
<div id="button">
<div id="sub-button">6</div>
</div>
<div style="clear:both"></div>
<div id="button2" style="margin-left:152px;">
<div id="sub-button2">66</div>
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/toggle.js"></script>
和JS代码(切换.js):
var myTimeout;
// show/hide sub-button menu
$( "#button" ).click(function() {
$( "#sub-button" ).toggle();
});
// if mouse out of button and sub-button divs - close sub-button after 860ms
$( "#button" ).mouseout(function() {
myTimeout = setTimeout( "jQuery('#sub-button').hide();",860 );
});
// if timer that are about to close sub-button is launched, stop it
// by hover button or sub-button divs
$( "#button" ).mouseover(function() {
clearTimeout(myTimeout);
});
var myTimeout2;
$( "#button2" ).click(function() {
$( "#sub-button2" ).toggle();
});
$( "#button2" ).mouseout(function() {
myTimeout2 = setTimeout( "jQuery('#sub-button2').hide();",860 );
});
$( "#button2" ).mouseover(function() {
clearTimeout(myTimeout2);
});
第一个问题是子菜单是#button
的子菜单,因此单击事件将冒泡到父级,并且将调用#button
click事件的代码。
您可以通过测试原始点击目标来解决的第一个问题:
$( "#button" ).click(function(e) {
if(e.target.id === 'button'){
$( "#sub-button" ).toggle();
}
});
第二个问题是,在您的代码中,您在单击第二个按钮时不会关闭第一个按钮,反之亦然
您可以添加:
$("#button").click(function (e) {
if (e.target.id === 'button') {
$("#sub-button2").hide();
$("#sub-button").toggle();
}
});
虽然如果你以后要有更多的按钮,这个JS可以而且应该整理并提高效率,否则你冒着代码变得难以维护的风险。
这是小提琴
只需像这样更改您的 js:
var myTimeout;
// show/hide sub-button menu
$( "#button" ).click(function() {
$("#sub-button2").hide();
if (!$("#sub-button2").is(":visible")) $( "#sub-button" ).show();
});
// if mouse out of button and sub-button divs - close sub-button after 860ms
$( "#button" ).mouseout(function() {
myTimeout = setTimeout( "jQuery('#sub-button').hide();",860 );
});
// if timer that are about to close sub-button is launched, stop it
// by hover button or sub-button divs
$( "#button" ).mouseover(function() {
clearTimeout(myTimeout);
});
var myTimeout2;
$( "#button2" ).click(function() {
if (!$("#sub-button2").is(":visible")) $( "#sub-button2" ).show();
$("#sub-button").hide();
});
$( "#button2" ).mouseout(function() {
myTimeout2 = setTimeout( "jQuery('#sub-button2').hide();",860 );
});
$( "#button2" ).mouseover(function() {
clearTimeout(myTimeout2);
});
如果我
理解正确,您希望防止事件气泡到父级,如果您单击菜单,请隐藏其他菜单子菜单。 无需做一堆"检查",只需正确处理事件即可。 请注意我在下面评论"添加"的位置
赶紧看实际的例子:http://jsfiddle.net/gP4CV/
var myTimeout;
// show/hide sub-button menu
$("#button").click(function () {
$("#sub-button").toggle();
$("#sub-button2").hide();// ADD hide other child
});
// ADD prevent event bubble to parent
$("#sub-button, #sub-button2").click(function (e) {
e.stopPropagation();
});
// if mouse out of button and sub-button divs - close sub-button after 860ms
$("#button").mouseout(function () {
myTimeout = setTimeout("jQuery('#sub-button').hide();", 860);
});
// if timer that are about to close sub-button is launched, stop it
// by hover button or sub-button divs
$("#button").mouseover(function () {
clearTimeout(myTimeout);
});
var myTimeout2;
$("#button2").click(function () {
$("#sub-button2").toggle();
$("#sub-button").hide(); // ADD hide other child
});
$("#button2").mouseout(function () {
myTimeout2 = setTimeout("jQuery('#sub-button2').hide();", 860);
});
$("#button2").mouseover(function () {
clearTimeout(myTimeout2);
});
相关文章:
- 捕获初始RedQueryBuilder查询中的SQL问题
- 解析.查询和组合问题
- j查询键盘问题上的事件
- 问题 - 在查询加载器 2 加载页面之前显示 1 秒的页面
- j查询网站标题问题
- Ajax发布到PHP脚本,每5秒查询一次MySQL数据库的性能/问题
- j查询模式窗口,刷新父问题
- j查询齿轮旋转问题
- 插入查询中的问题--sqlite Phonegap
- 对jQuery使用setTimeout()是否有任何问题;媒体查询“;类型情况
- 使用jquery从URL中删除查询字符串(处理问题)
- 解析云代码-在“”中查询用户问题;正常的“;作用
- mongoose中嵌套查询执行的问题
- j查询淡入和淡出问题
- 从 MongoDB 打印查询时出现问题
- 在调用 ajax 和查询数据库时遇到问题
- j查询滑块导航问题
- 删除导致页面刷新出现问题的查询参数
- j查询日历操作问题
- 更新的问题::查询选择器可能存在PhantomJS/Javascript问题