search
HomeWeb Front-endH5 Tutorial这两个js有冲突吗?

回复内容:

a = 1;
a = 2;

你说运行完 a 是多少? N首先,你这能用CSS完成绝大部分工作
其次,你可以组织一个高一点层次的入口函数去管理你onload到底执行什么,怎么执行,可以通过传入回调函数去扩展你的函数逻辑。
这语言太灵活了,快去感受感受 Window 的 onload 被第二个覆盖掉了。可以尝试 window.addEventListener('load', func...) 通过封装一个通用函数,可以避免这个问题,只要添加事件处理程序的时候使用该函数,window.onload就不会被覆盖。
<span class="kd">function</span> <span class="nx">addLoadHandler</span><span class="p">(</span><span class="nx">handler</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">old</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">onload</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">===</span> <span class="s1">'function'</span><span class="p">)</span> <span class="p">{</span>
       <span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
          <span class="nx">old</span><span class="p">();</span>
          <span class="nx">handler</span><span class="p">();</span> 
       <span class="p">}</span>     
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
       <span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span> <span class="nx">handler</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
多补点基础知识,多搜索,不要随便提问。你给 onload 赋值了 2 次,前次的值怎么可能还在?

<span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">a</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="nx">alert</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span>
window.onload = Fun1;
window.onload = Fun2;

很明显,把对象指向了新函数Fun2

utility.js
 function addOnloader(newFunc) {
        if (typeof window.onload !== 'function') {
            window.onload = newFunc;
        } else {
            var previousOnload = window.onload;
            window.onload = function() {
                previousOnload();
                newFunc();
            };
        }
    }

window.util.addOnloader = addOnloader;
只能有一个onload 我也来回答一下吧。问题很小白,但是我也是刚从小白出来的(想到自己的痛处,心软了...)。前面的如 @顾轶灵 和@Boring(@不到)说的很明白也很清楚,但是对于小白还是费解了一点。

这个还是基本功的原因,还是希望楼主多看一些基础的书籍,这种问题在论坛里或者QQ群里是基本不会有人回答而且还会被调侃小白或者说你不愿意去学习并且还是不会回答你问题的。我当时却会为这样的问题郁闷上两天。然后再去慢慢的找原因。

所以我想还是补充一下这个问题的答案。

以下是我的理解,不是专业书籍也不是专业语言。望轻喷。

原因:是因为同样的函数你赋值(或者叫做绑定函数)了两次,而这个函数是唯一的(在你这种写法的情况下),所以会后面的赋值覆盖了前面,自然只会执行后面的函数而且不会报错。

也就是说window.onload这个函数你赋值了两次。

这个东西大神也说过了,现成的如: 这两个js有冲突吗?
(截图源于《锋利的jQuery》第二版,如有侵权,请在评论中说明。)

问题回答完了。

现在解释一下 @learnshare 的说法:
概念:页面上任何一个元素的事件都是已经存在的,只是我们用的时候,拿来给我们想调用的事件赋值了一个函数(你上面贴出来的代码)或者绑定了一个函数(就是我现在解释的)。

这两张方式同样可以达到目的,但是还是有一些区别的。比如window.onload事件,如果用绑定的方式写,就不会冲突或者覆盖。

具体到写法上,还有兼容问题,这个我就不写了,你找本书看看吧。
<span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span>  <span class="kd">function</span><span class="p">(){</span>
   <span class="nx">a</span><span class="p">();</span>
   <span class="nx">b</span><span class="p">();</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">a</span><span class="p">(){</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'a'</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">b</span><span class="p">(){</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'b'</span><span class="p">)</span>
<span class="p">}</span>
你可以看下事件。直接使用onload,on等等的话后面定义会将前面覆盖,就像顾轶灵说的一样:a=1;a=2。所以事件绑定一般使用事件监听addeventlisten函数,这样就不会被覆盖。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
H5: The Future of Web Content and DesignH5: The Future of Web Content and DesignMay 01, 2025 am 12:12 AM

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5: New Features and Capabilities for Web DevelopmentH5: New Features and Capabilities for Web DevelopmentApr 29, 2025 am 12:07 AM

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

H5: Key Improvements in HTML5H5: Key Improvements in HTML5Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

HTML5: The Standard and its Impact on Web DevelopmentHTML5: The Standard and its Impact on Web DevelopmentApr 27, 2025 am 12:12 AM

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 Code Examples: Practical Applications and TutorialsH5 Code Examples: Practical Applications and TutorialsApr 25, 2025 am 12:10 AM

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

The Connection Between H5 and HTML5: Similarities and DifferencesThe Connection Between H5 and HTML5: Similarities and DifferencesApr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

The Building Blocks of H5 Code: Key Elements and Their PurposeThe Building Blocks of H5 Code: Key Elements and Their PurposeApr 23, 2025 am 12:09 AM

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

HTML5 and H5: Understanding the Common UsageHTML5 and H5: Understanding the Common UsageApr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!