jQuery 切换不切换
我有一个页面,其中有一堆假下拉菜单,其编码如下(我无法真正更改 HTML):
<div class="select">
<div class="wrapper calcdropdown">
<a href="#" class="dd-openercalc">Select a calculator</a>
<a href="#" class="dd-opener pulldown-arrow"> </a>
</div>
<ul class="selectbox">
<li>Dropdown item 1</li>
<li>Dropdown item 2</li>
<li>Dropdown item 3</li>
<ul>
</div>
当我单击 calcdropdown DIV 内的任一链接时,我需要切换相应的选择框 UL。当我使用toggle()时,当我第一次单击链接时,选择框 UL 会出现,但当我再次单击链接时,选择框 UL 不会消失。
var $j = jQuery.noConflict();
$j(document).ready(function() {
// hide all the dropdowns by default
$j(".selectbox").hide();
$j('.calcdropdown a').live('click',function(e) {
// hide the dropdowns in case another one's open
$j(".selectbox").hide();
var self = $j(this);
// show the selectbox for this link
var thisSelectbox = $j(this).parents('.select').children('.selectbox').toggle();
e.preventDefault();
});
});
这也不起作用:
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j(".selectbox").hide();
$j('.calcdropdown a').live('click',function(e) {
$j(".selectbox").hide();
var self = $j(this);
var thisSelectbox = $j(this).parents('.select').children('.selectbox');
if(thisSelectbox.is(':hidden')) {
thisSelectbox.show();
} else {
thisSelectbox.hide();
}
});
});
如何在显示选择框后隐藏它们?
I have a page with a bunch of fake dropdown menus that are coded like this (I can't really change the HTML):
<div class="select">
<div class="wrapper calcdropdown">
<a href="#" class="dd-openercalc">Select a calculator</a>
<a href="#" class="dd-opener pulldown-arrow"> </a>
</div>
<ul class="selectbox">
<li>Dropdown item 1</li>
<li>Dropdown item 2</li>
<li>Dropdown item 3</li>
<ul>
</div>
When I click on either of the links inside the calcdropdown DIVs I need to toggle the corresponding selectbox UL. When I use toggle() the selectbox UL appears when I first click the links but doesn't disappear when I click the links again.
var $j = jQuery.noConflict();
$j(document).ready(function() {
// hide all the dropdowns by default
$j(".selectbox").hide();
$j('.calcdropdown a').live('click',function(e) {
// hide the dropdowns in case another one's open
$j(".selectbox").hide();
var self = $j(this);
// show the selectbox for this link
var thisSelectbox = $j(this).parents('.select').children('.selectbox').toggle();
e.preventDefault();
});
});
This doesn't work, either:
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j(".selectbox").hide();
$j('.calcdropdown a').live('click',function(e) {
$j(".selectbox").hide();
var self = $j(this);
var thisSelectbox = $j(this).parents('.select').children('.selectbox');
if(thisSelectbox.is(':hidden')) {
thisSelectbox.show();
} else {
thisSelectbox.hide();
}
});
});
How can I hide the selectboxes after I show them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从点击函数中删除
$j(".selectbox").hide();
。否则,您总是在点击功能中执行以下步骤:
编辑:(我没有意识到您使用多个这些元素)
要隐藏除与单击相关的之外的所有 .selectbox,请使用 not():
http://jsfiddle.net/doktormolle/qG8ps/
Remove
$j(".selectbox").hide();
from the click-function.Otherwise you always do this steps inside the click-function:
Edit:(I didn't realize that you use more than one of those elements)
To hide all .selectbox except the one related to the click use not():
http://jsfiddle.net/doktormolle/qG8ps/
根据您对其他答案的评论,您需要一个解决方案:
这有点混乱,但如果您更改
$j(".selectbox").hide ();
在第一个示例中$j(".selectbox").not($j(this).parents('.select').children('.selectbox')).hide();
,你应该得到你所追求的行为。这将隐藏除与当前链接相对应的元素之外的所有
.selectbox
元素。然后,您对toggle()
的调用应该按预期工作。From your comments on the other answer, you're after a solution that:
It's a bit messy, but if you change
$j(".selectbox").hide();
in your first example to$j(".selectbox").not($j(this).parents('.select').children('.selectbox')).hide();
, you should get the behaviour you're after.This hides all
.selectbox
elements except the one that corresponds to the current link. Then, your call totoggle()
should work as expected.