aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/guide/html/event_attributes/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-tw/web/guide/html/event_attributes/index.html')
-rw-r--r--files/zh-tw/web/guide/html/event_attributes/index.html84
1 files changed, 84 insertions, 0 deletions
diff --git a/files/zh-tw/web/guide/html/event_attributes/index.html b/files/zh-tw/web/guide/html/event_attributes/index.html
new file mode 100644
index 0000000000..6f57526dde
--- /dev/null
+++ b/files/zh-tw/web/guide/html/event_attributes/index.html
@@ -0,0 +1,84 @@
+---
+title: Event attributes
+slug: Web/Guide/HTML/Event_attributes
+translation_of: >-
+ Learn/JavaScript/Building_blocks/Events#Inline_event_handlers_%E2%80%94_don%27t_use_these
+---
+<p><span class="seoSummary">每一個 HTML 元素都可以放置事件屬性,以藉此於事件發生時能執行 JavaScripte 程式。事件屬性的名稱都有一個前綴「on」,例如當使用者點選元素時要執行指定的 JavaScript,可以使用 </span><code>onclick</code><span class="seoSummary"> 屬性並把要執行的 JavaScript 當成屬性值。</span></p>
+
+<p>In the JavaScript code executed in response to the event, <code>this</code> is bound to the HTML element and the {{domxref("Event")}} object can be reached using the <code>event</code> variable in the scope of the attribute.</p>
+
+<div class="warning">
+<p><strong>Warning:</strong> These attributes should be avoided. This makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find. Furthermore, usage of event attributes almost always causes scripts to expose global functions on the {{domxref("Window")}} object, polluting the global namespace.</p>
+</div>
+
+<p>While these attributes can at times be attractively easy to use, you should avoid using them. Instead, use the {{domxref("EventTarget.addEventListener()")}} function to add a listener for the event.</p>
+
+<p>Event attributes can be blocked by using <a href="/en-US/docs/Security/CSP/Introducing_Content_Security_Policy">Content Security Policy</a> which if used, blocks all inline scripts unless the <em>'unsafe-inline'</em> keyword is used.</p>
+
+<h2 id="Example_using_event_attributes" name="Example_using_event_attributes">Example using event attributes</h2>
+
+<p>This example appends text to an element each time time the {{HTMLElement("div")}} is clicked.</p>
+
+<div class="note">
+<p><strong>Note:</strong> This is an example of how not to do things, using one of these attributes.</p>
+</div>
+
+<pre class="brush: html">&lt;!doctype html&gt;
+&lt;html&gt;
+ &lt;head&gt;
+ &lt;title&gt;Event Attribute Example&lt;/title&gt;
+ &lt;script&gt;
+ function doSomething() {
+ document.getElementById("thanks").innerHTML += "&lt;p&gt;Thanks for clicking!&lt;/p&gt;";
+ }
+ &lt;/script&gt;
+ &lt;/head&gt;
+ &lt;body&gt;
+ &lt;div onclick="doSomething();"&gt;Click me!&lt;/div&gt;
+ &lt;div id="thanks"&gt;&lt;/div&gt;
+ &lt;/body&gt;
+&lt;/html&gt;
+</pre>
+
+<p>Try this example below:</p>
+
+<p>{{ EmbedLiveSample('Example_using_event_attributes', '', '', '') }}</p>
+
+<h2 id="Example_using_event_listeners">Example using event listeners</h2>
+
+<p>Instead, you should use {{domxref("EventTarget.addEventListener()")}}, as shown here:</p>
+
+<pre class="brush: html">&lt;!doctype html&gt;
+&lt;html&gt;
+ &lt;head&gt;
+ &lt;title&gt;Event Attribute Example&lt;/title&gt;
+ &lt;script&gt;
+ function doSomething() {
+ document.getElementById("thanks").innerHTML += "&lt;p&gt;Thanks for clicking!&lt;/p&gt;";
+ }
+
+ // Called when the page is done loading; this is where we do any setup we need,
+ // such as creating event listeners for the elements in the page.
+
+ function setup() {
+ document.getElementById("click").addEventListener("click", doSomething, true);
+ }
+
+ // Install an event handler on the window to receive the "load" event, which
+ // indicates that the document has finished loading into the window.
+
+ window.addEventListener("load", setup, true);
+ &lt;/script&gt;
+ &lt;/head&gt;
+ &lt;body&gt;
+ &lt;div id="click"&gt;Click me!&lt;/div&gt;
+ &lt;div id="thanks"&gt;&lt;/div&gt;
+ &lt;/body&gt;
+&lt;/html&gt;</pre>
+
+<p>You can see this in action below:</p>
+
+<p>{{ EmbedLiveSample('Example_using_event_listeners', '', '', '') }}</p>
+
+<section id="Quick_Links"><ol><li><a href="/en-US/docs/Web/API/Event" title='The Event interface represents an event which takes place in the DOM; some are user-generated (such as mouse or keyboard events), while others are generated by APIs (such as events that indicate an animation has finished running, a video has been paused, and so forth). While events are usually triggered by such "external" sources, they can also be triggered programmatically, such as by calling the HTMLElement.click() method of an element, or by defining the event, then sending it to a specified target using EventTarget.dispatchEvent(). There are many types of events, some of which use other interfaces based on the main Event interface. Event itself contains the properties and methods which are common to all events.'>Event</a></li><li><a href="/en-US/docs/Web/API/EventTarget" title="EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.">EventTarget</a></li><li><a href="/en-US/docs/Web/API/EventTarget.addEventListener">EventTarget.addEventListener</a></li></ol></section>