uTools-Manuals/docs/jQuery/unload.html

88 lines
4.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.unload()</h1>
<div class="entry-meta">
所属分类:<span class="category"><a href="/category/events/">事件</a> &gt; <a href="/category/events/document-loading/">文档加载</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/deprecated/">弃用</a> &gt; <a href="/category/deprecated/deprecated-1.8/">1.8 版本弃用的 API</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/removed/">已删除的函数</a></span>
<span class="pull-right">英文文档:<a href="https://api.jquery123.com/unload/" target="_blank">.unload()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="unload1"><h2 class="section-title">
<span class="name">.unload( handler(eventObject) )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span><span class="version-details">version deprecated: <a href="/category/version/1.8/">1.8</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>为 JavaScript 的 "unload" 事件绑定一个处理函数。</p>
<ul class="signatures">
<li class="signature" id="unload-handlereventObject">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.unload( handler(eventObject) )</h4>
<ul><li>
<div><strong>handler(eventObject)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>每次事件触发时会执行的函数。</div>
</li></ul>
</li>
<li class="signature" id="unload-eventData-handlereventObject">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.4.3/">1.4.3</a></span>.unload( [eventData ], handler(eventObject) )</h4>
<ul>
<li>
<div><strong>eventData</strong></div>
<div>类型: <a href="/Types/#Object">Object</a>
</div>
<div>一个普通对象,它包含的数据键值对映射将被传递给事件处理程序。</div>
</li>
<li>
<div><strong>handler(eventObject)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>每次事件触发时会执行的函数。</div>
</li>
</ul>
</li>
</ul>
<div class="longdesc" id="entry-longdesc">
<p>这个方法是<code>.bind('unload', handler)</code>的简写。</p>
<p>当用户离开这个页面时,就会触发<code>window</code>元素上的<code>unload</code>事件。这意味着很多情况会触发这个事件。用户可以点击一个链接离开页面或在地址栏输入一个新的URL。前进和后退按钮会触发该事件。关闭浏览器窗口将导致该事件被触发。即使重新加载页面首先会创建一个<code>unload</code>事件。</p>
<blockquote>
<p><code>unload</code>事件的处理器在浏览器的各个版本中可能有变化的。例如Firefox的某些版本中会跟着一个链接触发该事件但当窗口被关闭时不会触发。在实际使用中应在所有支持的浏览器中进行测试和对比的专有<code>beforeunload</code>事件。</p>
</blockquote>
<p>任何 <code>unload</code>事件处理程序必须绑定到 <code>window</code> 对象:</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(window).unload(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code> alert(<span class="string">'Handler for .unload() called.'</span>);</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>这些代码执行后浏览器离开当前页面时将显示警报alert。不可能用事件对象的<code>.preventDefault()</code>方法取消<code>unload</code>事件。当用户离开该页面,需要使用脚本进行一些清理工作的场合,可以使用该事件。
</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">当离开页面时显示一个提示框:</span></h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(window).unload( <span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span> alert(<span class="string">"Bye now!"</span>); } );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>