开发者

Codes fail to run when not wrapped in an anonymous function in jquery

开发者 https://www.devze.com 2022-12-18 06:04 出处:网络
If you paste these codes http://paste.plurk.com/show/152772 in http://htmledit.squarefree.com/ You will see the codes run without any problem. The images altogether will turn into a slideshow.

If you paste these codes http://paste.plurk.com/show/152772 in http://htmledit.squarefree.com/

You will see the codes run without any problem. The images altogether will turn into a slideshow.

However, if you paste the following codes:

http://paste.pl开发者_JAVA百科urk.com/show/152773

The codes will fail to run, no slideshow.

These two pieces of codes only differ in whether the codes are wrapped inside a jquery anonymous function.

I just have no idea why the second piece of codes doesn't work.


Interesting. You might want to place the function in:

 $(document).ready(function() {
     //stuff to do on document.ready()
 });

It appears that the first example does that for you, which is why it works. The second example is probably performing those actions before the elements appear in the dom, which would make the javascript pointless.

Resource: About $(document).ready()


It's not an anonymous function, it's shorthand for

$(document).ready(function() {
})

See http://api.jquery.com/ready/

0

精彩评论

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