Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载所有元素的Javascript触发器_Javascript_Jquery_Html - Fatal编程技术网

加载所有元素的Javascript触发器

加载所有元素的Javascript触发器,javascript,jquery,html,Javascript,Jquery,Html,我有2个由此代码加载的外部html页面: <div id="header-div"></div> <div id="footer-div"></div> 我想在创建头文件中的特定元素时运行函数- 我可以用什么触发器来触发它 没关系,当所有元素都加载时会触发 谢谢 将回调添加到您的load()调用中 $(function () { $("#header-div").load("/AfekDent/header.html", functio

我有2个由此代码加载的外部html页面:

<div id="header-div"></div>

<div id="footer-div"></div>
我想在创建头文件中的特定元素时运行函数- 我可以用什么触发器来触发它

没关系,当所有元素都加载时会触发


谢谢

将回调添加到您的
load()
调用中

$(function () {
    $("#header-div").load("/AfekDent/header.html", function() {
        console.log('My header was loaded!');
    });
});

您可以在
#header div
上使用回调,它将在加载整个header后执行代码

$("#header-div").load("AfekDent/header.html", function() {
  someFunction();
});
但是,如果要在加载标头中的特定元素后执行代码,请尝试以下操作:

$("#specific-element").on("load", function() {
  someOtherFunction();
});
如果您想进一步了解
load
on(“load”)
之间的区别,或者阅读jQuery文档了解和

为了简单起见,我建议使用
$(document.ready(function(){yetAnotherFunction();})执行代码,但这取决于您的具体情况。

如果您阅读,您将看到
load()
函数接受一个
complete
回调,该回调将在加载完成时执行<代码>$(“#some div”).load(“page.html”,function(){alert(“load已执行”);})
$("#specific-element").on("load", function() {
  someOtherFunction();
});