views:

47

answers:

2

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.plurk.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.

+6  A: 

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()

Plan B
+4  A: 

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

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

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

Per Holmäng