Javascript variables - setting as percentage
<div class="contentBlockOne">
<span class="moreButton">More</span>
<br />Each block will have an artic开发者_JS百科le summary in it.</div>
I want to increase the width of "Div" on a "Span" click with the following jQuery ( I havn't bothered including the else part):
var currentWidth = $('.moreButton').parent().width();
$('.moreButton').click(function () {
if ($(this).parent().width == currentWidth) {
$(this).parent().animate({ width: '98%' }, 800);
$(this).text('Less');
}
In my head the "currentWidth" variaeble should be the same as the parents width. But when debugging, the code does not do anything. I presume it is because the, for what ever reason, the two width values are not the same.
You're missing parentheses on the .width()
call. Change:
if ($(this).parent().width == currentWidth) {
// to
if ($(this).parent().width() == currentWidth) {
精彩评论