[英]Javascript trigger for load of all elements
I have 2 external html pages loaded by this code: 我通过此代码加载了2个外部html页面:
<div id="header-div"></div>
<div id="footer-div"></div>
my js file contains this: 我的js文件包含以下内容:
$(function () {
$("#header-div").load("/AfekDent/header.html");
$("#footer-div").load("/AfekDent/footer.html");
});
I want to run a function when specific element in the header file is created - what trigger can i use for it? 创建头文件中的特定元素时,我想运行一个函数-我可以使用哪个触发器?
It's ok the trigger will occur when all elements will be loaded. 可以,当所有元素都被加载时,触发器就会发生。
thanks! 谢谢!
Add a callback to your load()
call. 在您的load()
调用中添加一个回调。
$(function () {
$("#header-div").load("/AfekDent/header.html", function() {
console.log('My header was loaded!');
});
});
You can use a callback on the #header-div
, which will execute the code after the entire header has loaded. 您可以在#header-div
上使用回调,该回调将在加载整个标头后执行代码。
$("#header-div").load("AfekDent/header.html", function() {
someFunction();
});
However, if you want to execute code after a specific element in the header loads , try something like: 但是,如果要在头文件中的特定元素加载后执行代码,请尝试以下操作:
$("#specific-element").on("load", function() {
someOtherFunction();
});
If you want to learn more about the difference between load
and on("load")
, look at this question or read the jQuery documentation for load and on() . 如果您想了解有关load
和on("load")
之间的区别的更多信息,请查看此问题或阅读jQuery文档中的load和on() 。
For simplicity, I'd recommend executing code with $(document).ready(function() {yetAnotherFunction();});
为简单起见,我建议使用$(document).ready(function() {yetAnotherFunction();});
执行代码$(document).ready(function() {yetAnotherFunction();});
, but it depends on your specific case. ,但这取决于您的具体情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.