JavaScript Reference/Javascript Methods/attachEvent

Материал из Web эксперт
Перейти к: навигация, поиск

"attachEvent()" Example

    
<html>
<body>
<button id="myButton">Button</button>
<button onclick="function3();">Apply an event handler "Button"</button>
<button onclick="function2();">Detach</button>
<script language="JavaScript">
    function function3() {
        document.all.myButton.attachEvent("onclick", function1)
    }
    function function1() {
        document.bgColor = "red";
    }
    function function2() {
        document.bgColor = "white"; 
        document.all.myButton.detachEvent("onclick", function1);
    }
</script>
</body>
</html>



"attachEvent()" is applied to

+----------------+--------------------------------------------------------------+
| Applied_To     |<a>                             <acronym>                     |
|                |<address>                       <applet>                      |
|                |<area>                          <b>                           |
|                |<base>                          <basefont>                    |
|                |<bgsound>                       <big>                         |
|                |<blockquote>                    <body>                        |
|                |<br>                            <button>                      |
|                |<caption>                       <center>                      |
|                |<cite>                          <code>                        |
|                |<col>                           <colgroup>                    |
|                |<comment>                       <custom>                      |
|                |<dd>                            <del>                         |
|                |<dfn>                           <dir>                         |
|                |<div>                           <dl>                          |
|                |document                        <dt>                          |
|                |<em>                            <embed>                       |
|                |<fieldset>                      <font>                        |
|                |<form>                          <frame>                       |
|                |<frameset>                      <head>                        |
|                |<hn>                            <hr>                          |
|                |<html>                          <i>                           |
|                |<iframe>                        <img>                         |
|                |<input type="button">           <input type="checkbox">       |
|                |<input type="file">             <input type="hidden">         |
|                |<input type="image">            <input type="password">       |
|                |<input type="radio">            <input type="reset">          |
|                |<input type="submit">           <input type="text">           |
|                |<ins>                           <kbd>                         |
|                |<label>                         <legend>                      |
|                |<li>                            <link>                        |
|                |<listing>                       <map>                         |
|                |<marquee>                       <menu>                        |
|                |namespace                       <nobr>                        |
|                |<object>                        <ol>                          |
|                |<option>                        <p>                           |
|                |<plaintext>                     <pre>                         |
|                |<q>                             <s>                           |
|                |<samp>                          <script>                      |
|                |<select>                        <small>                       |
|                |<span>                          <strike>                      |
|                |<strong>                        <sub>                         |
|                |<sup>                           <table>                       |
|                |<tbody>                         <td>                          |
|                |<textarea>                      <tfoot>                       |
|                |<th>                            <thead>                       |
|                |<title>                         <tr>                          |
|                |<tt>                            <u>                           |
|                |<ul>                            <var>                         |
|                |window                          <xmp>                         |
+----------------+--------------------------------------------------------------+



"attachEvent()" Syntax, Parameters and Note

Note:
Bound event to a function in the page. 
Returns true (successful) or false (failed).
    
Syntax:
    
window.attachEvent(param1, param2)
document.all.elementID.attachEvent(param1, param2)
Parameters:
    param1   Required; the name of the event.
    param2   Required; the name of the function.