Answers:
您可以有多个,但这并不总是最整洁的事情。尽量不要过度使用它们,因为这会严重影响可读性。除此之外,这是完全合法的。见下面:
http://www.learningjquery.com/2006/09/multiple-document-ready
试试看:
$(document).ready(function() {
alert('Hello Tom!');
});
$(document).ready(function() {
alert('Hello Jeff!');
});
$(document).ready(function() {
alert('Hello Dexter!');
});
您会发现它与此等效,请注意执行顺序:
$(document).ready(function() {
alert('Hello Tom!');
alert('Hello Jeff!');
alert('Hello Dexter!');
});
还值得注意的是,一个$(document).ready
块中定义的函数不能从另一个$(document).ready
块中调用,我只是运行了此测试:
$(document).ready(function() {
alert('hello1');
function saySomething() {
alert('something');
}
saySomething();
});
$(document).ready(function() {
alert('hello2');
saySomething();
});
输出为:
hello1
something
hello2
您可以使用多个。但是您也可以在一个文档中使用多种功能。
$(document).ready(function() {
// Jquery
$('.hide').hide();
$('.test').each(function() {
$(this).fadeIn();
});
// Reqular JS
function test(word) {
alert(word);
}
test('hello!');
});
是的,可以有多个$(document).ready()调用。但是,我认为您不知道将以哪种方式执行它们。(资源)
Ready handlers bound this way are executed after any bound by the other three methods above.
是的,有可能,但您最好使用div #mydiv并同时使用
$(document).ready(function(){});
//and
$("#mydiv").ready(function(){});
是的你可以。
如果您有其他使用同一文档的模块不在同一页面上,则多个文档就绪部分特别有用。使用旧的window.onload=func
声明,每次您指定要调用的函数时,它都会替换旧的。
现在,指定的所有功能都将排队/堆叠(有人可以确认吗?),而无论在哪个文档准备区中指定它们。
我认为最好的方法是将开关切换到命名函数(有关此主题的更多信息,请检查此溢出)。这样,您可以从单个事件中调用它们。
像这样:
function firstFunction() {
console.log("first");
}
function secondFunction() {
console.log("second");
}
function thirdFunction() {
console.log("third");
}
这样,您可以在单个就绪函数中加载它们。
jQuery(document).on('ready', function(){
firstFunction();
secondFunction();
thirdFunction();
});
这会将以下内容输出到您的console.log:
first
second
third
这样,您可以将功能重用于其他事件。
jQuery(window).on('resize',function(){
secondFunction();
});
这是合法的,但有时会导致不良行为。作为示例,我使用了MagicSuggest库,并在项目的页面中添加了两个MagicSuggest输入,并为每个输入的初始化使用了独立的文档就绪函数。第一个Input初始化有效,但第二个无效,也没有给出任何错误,Second Input未显示。因此,我始终建议使用一个“文档就绪”功能。
您甚至可以将文档就绪功能嵌套在随附的html文件中。这是使用jquery的示例:
档案:test_main.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="jquery-1.10.2.min.js"></script>
</head>
<body>
<div id="main-container">
<h1>test_main.html</h1>
</div>
<script>
$(document).ready( function()
{
console.log( 'test_main.html READY' );
$("#main-container").load("test_embed.html");
} );
</script>
</body>
</html>
档案:test_embed.html
<h1>test_embed.html</h1>
<script>
$(document).ready( function()
{
console.log( 'test_embed.html READY' );
} );
</script>
控制台输出:
test_main.html READY test_main.html:15
test_embed.html READY (program):4
浏览器显示:
test_embed.html
您也可以通过以下方式进行操作:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("#test").hide();
});
$("#show").click(function(){
$("#test").show();
});
});
</script>
</head>
<body>
<h2>This is a test of jQuery!</h2>
<p id="test">This is a hidden paragraph.</p>
<button id="hide">Click me to hide</button>
<button id="show">Click me to show</button>
</body>
先前的答案显示在单个.ready块中使用多个命名函数,或者在.ready块中使用单个未命名函数,而在.ready块之外使用另一个命名函数。我在研究.ready块中是否可以使用多个未命名函数的方法时发现了这个问题-我无法获得正确的语法。我终于想通了,并希望通过发布测试代码,我可以帮助其他人寻找我遇到的相同问题的答案