这两个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

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

This article explains how to embed audio in HTML5 using the <audio> element, including best practices for format selection (MP3, Ogg Vorbis), file optimization, and JavaScript control for playback. It emphasizes using multiple audio f

This article explains how to create and validate HTML5 forms. It details the <form> element, input types (text, email, number, etc.), and attributes (required, pattern, min, max). The advantages of HTML5 forms over older methods, incl

The article discusses managing user location privacy and permissions using the Geolocation API, emphasizing best practices for requesting permissions, ensuring data security, and complying with privacy laws.

The article discusses using the HTML5 Page Visibility API to detect page visibility, improve user experience, and optimize resource usage. Key aspects include pausing media, reducing CPU load, and managing analytics based on visibility changes.

The article discusses using viewport meta tags to control page scaling on mobile devices, focusing on settings like width and initial-scale for optimal responsiveness and performance.Character count: 159

This article details creating interactive HTML5 games using JavaScript. It covers game design, HTML structure, CSS styling, JavaScript logic (including event handling and animation), and audio integration. Essential JavaScript libraries (Phaser, Pi

The article explains how to use the HTML5 Drag and Drop API to create interactive user interfaces, detailing steps to make elements draggable, handle key events, and enhance user experience with custom feedback. It also discusses common pitfalls to a

This article explains the HTML5 WebSockets API for real-time, bidirectional client-server communication. It details client-side (JavaScript) and server-side (Python/Flask) implementations, addressing challenges like scalability, state management, an
