这两个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>
<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 = 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;
这个还是基本功的原因,还是希望楼主多看一些基础的书籍,这种问题在论坛里或者QQ群里是基本不会有人回答而且还会被调侃小白或者说你不愿意去学习并且还是不会回答你问题的。我当时却会为这样的问题郁闷上两天。然后再去慢慢的找原因。
所以我想还是补充一下这个问题的答案。
以下是我的理解,不是专业书籍也不是专业语言。望轻喷。
原因:是因为同样的函数你赋值(或者叫做绑定函数)了两次,而这个函数是唯一的(在你这种写法的情况下),所以会后面的赋值覆盖了前面,自然只会执行后面的函数而且不会报错。
也就是说window.onload这个函数你赋值了两次。
这个东西大神也说过了,现成的如:

(截图源于《锋利的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>

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

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

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

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.

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.

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.

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

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.

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.

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.

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.
