我有一個元素最初被內聯樣式顯示隱藏:無。點擊另一個元素我想顯示隱藏的元素,然後重新點擊我想再次隱藏它。但是,如果我在else語句中放入任何不顯示或隱藏的東西,但是如果我留下其他空白,它將顯示該元素。jquery 1.7.1隱藏並顯示工作不正常
也toggle();和.is(:hidden)也不工作。
jQuery的版本是jQuery的v1.6.4或jQuery的V1.7.1
什麼錯?
//this does not work
$('#cust_select').click(function(e) {
var element = document.getElementById('cust_list');
if($('#cust_list').css('display') === 'none') {
$('#cust_list').show();
}
else if($('#cust_list').css('display') !== 'none'){
$('#cust_list').hide();
}
//this will show the element
$('#cust_select').click(function(e) {
var element = document.getElementById('cust_list');
if($('#cust_list').css('display') === 'none') {
$('#cust_list').show();
}
else if($('#cust_list').css('display') !== 'none'){
//do nothing
}
});
HTML:
<ul id="selectLinkTop" class="clickMenu selectmenu SugarActionMenu" name="">
<li class="sugar_action_button">
<input id="checkallContacts" class="checkbox massall" type="checkbox" name="checkallContacts" style="float: left;margin: 2px 0 0 4px;" onclick="">
<ul id="cust_list" style="background: none repeat scroll 0 0 #FFFFFF;border: 1px solid #CCCCCC;box-shadow: 0 5px 10px #999999;float: left;left: 0;list-style: none outside none;margin: 0;overflow: hidden;padding: 8px 0;position: absolute;top: 18px;width: auto;z-index: 10;display: none;">
<li style="clear: both;margin: 0;padding: 0;white-space: nowrap;width: 100%;"><a id="button_select_this_page_top" style="border: 0 none !important;float: left;font-size: 12px !important;padding: 1px 10px !important;text-align: left;width: 100%;line-height: 18px;display: block;" href="#">Select This Page</a></li>
<li style="clear: both;margin: 0;padding: 0;white-space: nowrap;width: 100%;"><a id="button_select_all_top" style="border: 0 none !important;float: left;font-size: 12px !important;padding: 1px 10px !important;text-align: left;width: 100%;line-height: 18px;display: block;" href="#" name="selectall">Select All</a></li>
<li style="clear: both;margin: 0;padding: 0;white-space: nowrap;width: 100%;"><a id="button_deselect_top" style="border: 0 none !important;float: left;font-size: 12px !important;padding: 1px 10px !important;text-align: left;width: 100%;line-height: 18px;display: block;" href="#" name="deselect">Deselect All</a></li>
</ul>
<span id="cust_select" class="subhover"> </span>
</li>
</ul>'
請make [js-fiddle](http://jsfiddle.net/)或[js-bin](http://jsbin.com/),以便更容易我們用你的代碼破解 – Ygg
使用'$('#cust_list')。is(':visible')'來測試一個元素是否可見 –
你缺少});第一個結尾 – Arunkumar