Table of Contents
回复内容:
Home Web Front-end H5 Tutorial 这两个js有冲突吗?

这两个js有冲突吗?

Jun 07, 2016 am 08:42 AM

回复内容:

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>
Copy after login
多补点基础知识,多搜索,不要随便提问。你给 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>
Copy after login
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;
Copy after login
只能有一个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>
Copy after login
你可以看下事件。直接使用onload,on等等的话后面定义会将前面覆盖,就像顾轶灵说的一样:a=1;a=2。所以事件绑定一般使用事件监听addeventlisten函数,这样就不会被覆盖。
Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How to make h5 click icon How to make h5 click icon Apr 06, 2025 pm 12:15 PM

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

What Does H5 Refer To? Exploring the Context What Does H5 Refer To? Exploring the Context Apr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

What is the H5 programming language? What is the H5 programming language? Apr 03, 2025 am 12:16 AM

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

How to make pop-up windows with h5 How to make pop-up windows with h5 Apr 06, 2025 pm 12:12 PM

H5 pop-up window creation steps: 1. Determine the triggering method (click, time, exit, scroll); 2. Design content (title, text, action button); 3. Set style (size, color, font, background); 4. Implement code (HTML, CSS, JavaScript); 5. Test and deployment.

What are the advantages of H5 page production What are the advantages of H5 page production Apr 05, 2025 pm 11:48 PM

The advantages of H5 page production include: lightweight experience, fast loading speed, and improving user retention. Cross-platform compatibility, no need to adapt to different platforms, improving development efficiency. Flexibility and dynamic updates, no audit required, making it easier to modify and update content. Cost-effective, lower development costs than native apps.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

See all articles