kir*_*cha 1 jquery show toggle hide html-lists
我有一个页面,上面有一堆像这样编码的假下拉菜单(我无法真正更改 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>
Run Code Online (Sandbox Code Playgroud)
当我单击 calcdropdown DIV 中的任一链接时,我需要切换相应的选择框 UL。当我使用 toggle() 时,选择框 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();
});
});
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
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();
}
});
});
Run Code Online (Sandbox Code Playgroud)
显示后如何隐藏选择框?
$j(".selectbox").hide();
从点击功能中删除。
否则,您总是在点击功能中执行以下步骤:
编辑:(我没有意识到您使用了多个这些元素)
要隐藏除与点击相关的所有 .selectbox 使用 not():
var $j = jQuery.noConflict();
$j(document).ready(function() {
// hide all the dropdowns by default
$j(".selectbox").hide();
$j('.calcdropdown a').live('click',function(e) {
var target=$j(this).parents('.select').children('.selectbox');
$j('.selectbox').not(target.toggle()).hide();
e.preventDefault();
});
});
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/doktormolle/qG8ps/