当 jquery、javascript 函数被调用时? [英] when jquery, javascript functions are called?

查看:43
本文介绍了当 jquery、javascript 函数被调用时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当HTML页面打开时,我可以通过多种方式调用javascript函数.(自动)
onpageshowonload$(function() {...} );,只有简单的行函数等.

When HTML page is open, I can call javascript function with many ways.(automatically)
onpageshow, onload, $(function() {...} );, only simple line function, etc..

我想知道它们的不同之处和顺序.
(最近,我知道如果页面正在缓存,onload 不会被调用,所以我将其更改为 onpageshow).

I want to know what are different and sequence of them.
(Recently, I understand onload don't called if the page is caching, so i change it onpageshow).

under code 中的[Number] 是我预期的顺序.

[Number] in under code is the sequence that I expected.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body onpageshow="onPageShow();" onload="onLoad()">

<script type="text/javascript">
  console.log("[1] - simple function");

  function onPageShow() {
    console.log("[2] - onPageShow()");
  }

  function onLoad() {
    console.log("[3] - onLoad()");
  }

  $(function() {
    console.log(`[4] - $(function(){})`);
    // same with document.onload, maybe
  });
</script>
</body>

推荐答案

OnLoad()

onload 事件在对象被加载时发生.Onload 最常用于元素中,用于在网页完全加载所有内容(包括图像、脚本文件、CSS 文件等)后执行脚本.

OnLoad()

The onload event occurs when an object has been loaded. Onload is most often used within the element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

示例(HTML 格式)

<element onload="myScript">

示例(在 JavaScript 中)

object.onload = function(){myScript};

OnPageShow()

onpageshow 事件在用户导航到网页时发生.onpageshow 事件与 onload 事件类似,不同之处在于它发生在页面首次加载时的 onload 事件之后.此外,每次加载页面时都会发生 onpageshow 事件,而从缓存加载页面时不会发生 onload 事件.

OnPageShow()

The onpageshow event occurs when a user navigates to a webpage. The onpageshow event is similar to the onload event, except that it occurs after the onload event when the page first loads. Also, the onpageshow event occurs every time the page is loaded, whereas the onload event does not occur when the page is loaded from the cache.

示例:

<body onpageshow="myFunction()">

这篇关于当 jquery、javascript 函数被调用时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆