问题
I've tried is(':visible')
, is(':hidden')
and css('display')
but I can not get the display status of an element after it has been through .slideToggle()
What am I missing?
Here is the jsfiddle: http://jsfiddle.net/djlerman/jbNg3/1/
and the code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Slide Toggle</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
<style type="text/css">
.ui-icon {
float:left;
}
</style>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('span#expandList').click(function () {
if ($(this).attr('class') == "ui-icon ui-icon-triangle-1-s") {
$(this).attr('class', "ui-icon ui-icon-triangle-1-e");
} else {
$(this).attr('class', "ui-icon ui-icon-triangle-1-s");
}
$(this.parentNode).nextAll('ul').eq(0).slideToggle();
$('#debug').html($(this.parentNode).nextAll('ul').eq(0).attr('id') + ' is(:visible): ' + $(this.parentNode).nextAll('ul').eq(0).is(':visible') + '<br />' +
$(this.parentNode).nextAll('ul').eq(0).attr('id') + ' is(:hidden): ' + $(this.parentNode).nextAll('ul').eq(0).is(':hidden') + '<br />' +
$(this.parentNode).nextAll('ul').eq(0).attr('id') + ' .css(display): ' + $(this.parentNode).nextAll('ul').eq(0).css('display') + '<br />'
);
})
});
</script>
</head>
<body >
<h3>
<span id="expandList" class="ui-icon ui-icon-triangle-1-s" ></span>
Heading 1
</h3>
<ul id="heading1">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<h3>
<span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 2
</h3>
<ul id="heading2">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<h3>
<span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 3
</h3>
<ul id="heading3">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<div id="debug"></div>
</body>
</html>
回答1:
wait for slideToggle to finish before checking status.
$(this.parentNode).nextAll('ul').eq(0).slideToggle(
(function () {
$('#debug').html($(this.parentNode).nextAll('ul').eq(0).attr('id') + ' is(:visible): ' + $(this.parentNode).nextAll('ul').eq(0).is(':visible') + '<br />' +
$(this.parentNode).nextAll('ul').eq(0).attr('id') + ' is(:hidden): ' + $(this.parentNode).nextAll('ul').eq(0).is(':hidden') + '<br />' +
$(this.parentNode).nextAll('ul').eq(0).attr('id') + ' .css(display): ' + $(this.parentNode).nextAll('ul').eq(0).css('display') + '<br />'
);
}).bind(this)
);
回答2:
Once gp. mentioned the concept of the "event finishing" I was able to find this thread.
jQuery wait for event to complete
I didn't know that I didn't know about this concept and how it related to the issue I was having. Thank you for the incite.
Here is the updated jsFiddle: http://jsfiddle.net/djlerman/jbNg3/3/
and the updated code.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Slide Toggle</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
<style type="text/css">
.ui-icon {
float:left;
}
</style>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('span#expandList').click(function () {
if ($(this).attr('class') == "ui-icon ui-icon-triangle-1-s") {
$(this).attr('class', "ui-icon ui-icon-triangle-1-e");
} else {
$(this).attr('class', "ui-icon ui-icon-triangle-1-s");
}
$(this.parentNode).nextAll('ul').eq(0).slideToggle( function () {
$('#debug').html($(this).attr('id') + ' is(:visible): ' + $(this).is(':visible') + '<br />' +
$(this).attr('id') + ' is(:hidden): ' + $(this).is(':hidden') + '<br />' +
$(this).attr('id') + ' .css(display): ' + $(this).css('display') + '<br />'
);
}).bind(this);
})
});
</script>
</head>
<body >
<div id="debug"><br /><br /><br /></div>
<hr />
<h3>
<span id="expandList" class="ui-icon ui-icon-triangle-1-s" ></span>
Heading 1
</h3>
<ul id="heading1">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<h3>
<span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 2
</h3>
<ul id="heading2">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<h3>
<span id="expandList" class="ui-icon ui-icon-triangle-1-s"></span>Heading 3
</h3>
<ul id="heading3">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</body>
</html>
来源:https://stackoverflow.com/questions/19253648/get-display-status-element-effected-by-slidetoggle