开发者

Why is my jQuery not running?

I'm sure I'm just having a brain fart and I'm missing something obvious, but please help.

I can't figure out why the following code (the early coding of a custom slide show) is not ruining when the document loads.

$(document).ready(function() {

    var numSlides = $('#slides .slide').length();
    var wrapperWidth开发者_StackOverflow社区 = $('#homeBanner').width();
    var totalWidth = numSlides * wrapperWidth;

    // set width of #slides to width of all .slide elements added together
    $('#slides').css('width', totalWidth+'px');

}); // end document.ready()


Length is a property, not a method.

var numSlides = $('#slides .slide').length; // note, no parens


To get the number of slides on the web page you should use either jQuery's .size() method or .length property. if I remember correctly you may not call length as a method.

var numSlides = $('#slides .slide').size();

By the way, .width() doubles as a setter and getter if you are working with pixel values, so you may simplify your last line that edits the CSS property as so.

$('#slides').width(totalWidth);

You might not want to use the dollar sign ($) unless you are positive that jQuery is the only JavaScript library for your project. You could replace the first dollar sign with 'jQuery' and then add the dollar sign into the anonymous function parameters. This way you can still use the dollar sign safely in the document.ready() method. Example:

jQuery(document).ready(function($) { /*code here*/ });

Hope that helps you fix your problem, good luck and let me know if you need anymore help.

Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜