On 14 Dez., 04:47, "Erik Beeson" <[EMAIL PROTECTED]> wrote:
> Then ele will just be available for the scope of your closure. Also, if you
> really are doing something like your example, you might as well cache the
> jQuery object instead of the selector to save having to look it up twice. A
> good convention is to start variable names that point to a jQuery object
> with $, like so:
>
> (function() {
>   var $ele = $('#foo');
>   $(document).ready(function() {
>        $ele. // ... Do something with 'ele'...
>   });
>   $(window).load(function () {
>        $ele. // ... Do something with 'ele'...
>   });
>
> })();

Unless you're putting that script at the bottom of the page - which
would make the ready event obsolete - $ele will be empty here, because
at the time that statement is executed the DOM is still not loaded.
Try:

(function() {
  var $ele;
  $(document).ready(function() {
       $ele = $('#foo');
       $ele. // ... Do something with 'ele'...
  });
  $(window).load(function () {
       $ele. // ... Do something with 'ele'...
  });
})();

That works because ready is fired before load. Still that doesn't look
all too robust to me. I'd go for the attach load event inside the
ready handler variant:

$(function() {
  var $foo = $('#foo');
  // ... Do something with $foo

  $(window).load(function () {
       // ... Do something with $foo
  });

})();

--Klaus

Reply via email to