javaScript and jQuery implement automatic loading
In this article, we will introduce to you the simplest code writing method to implement automatic loading using javaScript and jQuery. Hope it helps everyone.
1. JavaScript automatic loading
①Use onload in text: Execute onload after all content (including images) in the page has been loaded, as follows:
<body onload="alert(1)"></body> <!-- 当有一个onload --> <body onload="alert(2);alert(3);alert(4)"></body> <!-- 当有多个onload用分号隔开,依次弹出 2 3 4 -->
②Use window.onload in the script: Execute window.onload after all the content in the page (including pictures) has been loaded, as follows:
window.onload = function(){...}; //正确写法,这是匿名函数 //------------↓多个window.onload的错误写法------------- window.onload = function(){alert("text1");}; //不执行 window.onload = function(){alert("text2");}; //执行 //------------↑--------------------------------------- //------------↓多个window.onload的正确写法--------------------------- window.attachEvent("onload",function(){alert('a')}); window.attachEvent("onload",function(){alert('b')}); window.attachEvent("onload",function(){alert('c')}); //重点提示:在IE浏览器下用 (window.attachEvent),会弹出 c b a //重点提示:其他浏览器下用 (window.addEventListener),会弹出 a b c //------------↑-----------------------------------------------------
2. jQuery automatic loading
①After the DOM structure (excluding images) in the page is loaded Execute again (maybe the things associated with the DOM element have not been loaded), there are three ways to write, as follows:
$(document).ready(function(){...});//写法1,全称 $(function(){...}); //写法2,简写 jQuery(function($){...}); //写法3,简写
② All in the page It is executed only after the elements (including images) are loaded, as follows.
$(window).load(function() {...}); //等于JavaScript的写法,如window.onload = function(){...};
③Execute the anonymous function immediately. When an anonymous function is enclosed in brackets and then followed by parentheses, the anonymous function can be run immediately. There are two ways to write it, as follows:
(function( ){...})(); //Writing method 1, without adding parameters
(function($){...})(jQuery); //Writing method 2, adding parameters to avoid conflicts with other variables .
Related recommendations:
Detailed examples of PHP automatic loading
Detailed examples of several automatic loading methods of classes in PHP
javascript firefox automatically loads iframe and automatically adjusts height and width example
The above is the detailed content of javaScript and jQuery implement automatic loading. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Introduction to JS-Torch JS-Torch is a deep learning JavaScript library whose syntax is very similar to PyTorch. It contains a fully functional tensor object (can be used with tracked gradients), deep learning layers and functions, and an automatic differentiation engine. JS-Torch is suitable for deep learning research in JavaScript and provides many convenient tools and functions to accelerate deep learning development. Image PyTorch is an open source deep learning framework developed and maintained by Meta's research team. It provides a rich set of tools and libraries for building and training neural network models. PyTorch is designed to be simple, flexible and easy to use, and its dynamic computation graph features make
