admin管理员组文章数量:1313347
I am working on a web page that pulls an external javascript file that has all of my functions in it. I'll call that file "functions.js".
functions.js has a jQuery has a $(function(){...});
to do my operations when the page is ready. My question is, is it possible to also write another $(function(){...}
on the body of the same page that calls functions.js, so that whatever I do in both of the domReady functions happens on the page? For example, if functions.js is:
$(function(){
$('div').css('color','green');
}
and I put this code in tags on the page which calls functions.js:
$(function(){
$('div').css('background-color','red');
}
will the page end up making my divs have both green text AND red backgrounds, or will one override the other, or will neither work?
I hope this makes sense!
I am working on a web page that pulls an external javascript file that has all of my functions in it. I'll call that file "functions.js".
functions.js has a jQuery has a $(function(){...});
to do my operations when the page is ready. My question is, is it possible to also write another $(function(){...}
on the body of the same page that calls functions.js, so that whatever I do in both of the domReady functions happens on the page? For example, if functions.js is:
$(function(){
$('div').css('color','green');
}
and I put this code in tags on the page which calls functions.js:
$(function(){
$('div').css('background-color','red');
}
will the page end up making my divs have both green text AND red backgrounds, or will one override the other, or will neither work?
I hope this makes sense!
Share Improve this question asked Apr 12, 2011 at 2:03 ZakZak 2,6984 gold badges30 silver badges46 bronze badges 1- 1 i asked the same question here , stackoverflow./questions/4394346/… – kobe Commented Apr 12, 2011 at 2:09
4 Answers
Reset to default 5Yes, you can add as many $(document).ready() calls as you want: http://docs.jquery./Tutorials:Multiple_$(document).ready()
You could just try it you know (it would have taken less time than it did for you to type all of that out) :)
Yes, both will run and work.
Yes you can write as many ready functions as you like. You Do do not really need ready functions if you place your code before closing </body>
tag and before jquery.js script.
Here's a well written blog on how document functions can slow you down and why they shouldn't be used. http://encosia./2010/08/18/dont-let-jquerys-document-ready-slow-you-down/
You can place as many as you like.
You can even place multiple $(window).load(function() { ... })
in jQuery (thanks cwolves).
In JavaScript (without jQuery), a workaround must be used...
(function() {
var events = [];
var registerWindowLoadEvent = function(callback) {
events.push(callback);
};
window.onload = function() {
for (var i = 0, eventsLength = events.length; i < eventsLength; i++) {
events[i].call();
};
};
})();
本文标签: javascriptCan you define jquery39s (function()) twice on a pageStack Overflow
版权声明:本文标题:javascript - Can you define jquery's $(function(){...}) twice on a page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741942069a2406201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论