共用方式為


Sys.UI.DomEvent.$addHandlers Method

Provides a shortcut to the addHandlers method of the Sys.UI.DomEvent class. This member is static and can be invoked without creating an instance of the class.

$addHandlers(element, events, handlerOwner, autoRemove);

Parameters

Term

Definition

element

The DOM element that exposes the event.

events

A dictionary of events and their handlers.

handlerOwner

(Optional) The object instance that is the context for the delegates that should be created from the handlers.

autoRemove

(Optional) A boolean value that determines whether the handler should be removed automatically when the element is disposed.

Exceptions

Exception type

Condition

Error.invalidOperation Function

(Debug) One of the handlers in events is not a function.

Remarks

For details about the method that this shortcut represents, see Sys.UI.DomEvent addHandlers Method.

Example

The following example shows how to associate an event handler with an element by using the $addHandlers shortcut method.

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title>$addHandlers Example</title>
</head>
<body>
    <h1>$addHandlers Example</h1>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"/>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Panel ID="Panel1" runat="server" GroupingText="Update Panel">
                   <asp:Label ID="Label1" runat="server" Text="Move cursor over button and click to see event types."></asp:Label>
                   <br />
                   <asp:Button ID="Button1" runat="server" Text="Button" AccessKey="b"  />
                   <br />
                   <asp:Label ID="Label2" runat="server"></asp:Label>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

<script type="text/javascript">
    $addHandlers($get("Button1"), {click:processEventInfo,mouseover:processEventInfo,mouseout:processEventInfo});

    // Event handler displays types for multiple events
    function processEventInfo(eventElement) {
        var result = '';
        result += eventElement.type;
        $get('Label2').innerHTML = result;
    }
 </script>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
    <title>$addHandlers Example</title>
</head>
<body>
    <h1>$addHandlers Example</h1>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"/>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Panel ID="Panel1" runat="server" GroupingText="Update Panel">
                   <asp:Label ID="Label1" runat="server" Text="Move cursor over button and click to see event types."></asp:Label>
                   <br />
                   <asp:Button ID="Button1" runat="server" Text="Button" AccessKey="b"  />
                   <br />
                   <asp:Label ID="Label2" runat="server"></asp:Label>
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

<script type="text/javascript">
    $addHandlers($get("Button1"), {click:processEventInfo,mouseover:processEventInfo,mouseout:processEventInfo});

    // Event handler displays types for multiple events
    function processEventInfo(eventElement) {
        var result = '';
        result += eventElement.type;
        $get('Label2').innerHTML = result;
    }
 </script>

See Also

Reference

Sys.UI.DomEvent Class

Other Resources

Language Reference