问题
Using jQuery 1.4.2 from Google hosted Code.
Is there a reason why the following javascript does not fire all 3 document.ready functions when the document is ready?
The first $(document).ready()
function, which renders headers, and the second, which gives a 'Foo' alert box triggered, but subsequent ones in new <script>
blocks aren't triggered,
<script type="text/javascript">
$(document).ready(function () {
Cufon.replace('h1'); // Works without a selector engine
Cufon.replace('h2'); // Works without a selector engine
Cufon.replace('h3'); // Works without a selector engine
Cufon.now();
});
$(document).ready(function () { alert("Number Foo"); });
</script>
// html tags
<script type="text/javascript">
$(document).ready(function () { alert("Number One"); });
$(document).ready(function () { alert("Number Two"); });
</script>
These are in seperate web parts, hosted on the same page in Sharepoint2010
回答1:
I can think of three forensic things to try, right off:
- try it with non-google-hosted libraries.
- comment out the Cufon calls -- I believe Cufon does some crazy stuff to download additional resources, yes? That may be interfering.
- sub in
$(window).load()
for one or more of your$(document).ready()
callback defs. They have different firing criteria --$(window).load()
waits for everything to load up, allegedly -- but the substitution may be revealing.
Of course, console.log()
and alert()
will be your in-leu-of-debugger-breakpoint best friends in this case.
回答2:
you're missing a closing curly bracket and parenthesis in the second script tag
回答3:
You are missing a });
in the end of the last $(document).ready
Once you correct this it should work
EDIT: Since you say now that each script tag is in a separate web part I believe the problem itself is not in the scripts. Something else in your page is messing up your code.
来源:https://stackoverflow.com/questions/3974675/jquery-document-ready-not-firing