共用方式為


Sys.UI.DomEvent target Field

Gets the object that the event acted on.

var targetVar = domEventVar.target;

Return Value

An object that represents the target that the event acted on.

Remarks

Use the target field to get the object that raised the event. For example, you can use the target field to get details such as the ID of the element that raised the event.

Example

The following example shows how to use the target field to retrieve the target element.

<script type="text/javascript">
    Sys.UI.DomEvent.addHandler($get("Button1"), "click", processEventInfo);

    function processEventInfo(eventElement) {
        var result = '';
        try {
            result += "altKey = " + eventElement.altKey + "<br/>";
            result += "button = " + eventElement.button + "<br/>";
            result += "keyCode = " + eventElement.keyCode + "<br/>";
            result += "clientX = " + eventElement.clientX + "<br/>";
            result += "clientY = " + eventElement.clientY + "<br/>";
            result += "ctrlKey = " + eventElement.ctrlKey + "<br/>";
            result += "offsetX = " + eventElement.offsetX + "<br/>";
            result += "offsetY = " + eventElement.offsetY + "<br/>";
            result += "screenX = " + eventElement.screenX + "<br/>";
            result += "screenY = " + eventElement.screenY + "<br/>";
            result += "shiftKey = " + eventElement.shiftKey + "<br/>";
            result += "target.id = " + eventElement.target.id + "<br/>";
            result += "type = " + eventElement.type + "<br/>";
        }
        catch(e) {
            alert("Error:" + e.message);
        }
        $get('Label2').innerHTML = result;
    }
 </script>
<script type="text/javascript">
    Sys.UI.DomEvent.addHandler($get("Button1"), "click", processEventInfo);

    function processEventInfo(eventElement) {
        var result = '';
        try {
            result += "altKey = " + eventElement.altKey + "<br/>";
            result += "button = " + eventElement.button + "<br/>";
            result += "keyCode = " + eventElement.keyCode + "<br/>";
            result += "clientX = " + eventElement.clientX + "<br/>";
            result += "clientY = " + eventElement.clientY + "<br/>";
            result += "ctrlKey = " + eventElement.ctrlKey + "<br/>";
            result += "offsetX = " + eventElement.offsetX + "<br/>";
            result += "offsetY = " + eventElement.offsetY + "<br/>";
            result += "screenX = " + eventElement.screenX + "<br/>";
            result += "screenY = " + eventElement.screenY + "<br/>";
            result += "shiftKey = " + eventElement.shiftKey + "<br/>";
            result += "target.id = " + eventElement.target.id + "<br/>";
            result += "type = " + eventElement.type + "<br/>";
        }
        catch(e) {
            alert("Error:" + e.message);
        }
        $get('Label2').innerHTML = result;
    }
 </script>

See Also

Reference

Sys.UI.DomEvent Class

Other Resources

Language Reference