SoFunction
Updated on 2025-03-03

Two good ways to get event source in javascript event function

In the event response of javascript, we need to obtain the event source object to change its properties, such as changing the src attribute of img. There are two ways to obtain event sources in event response functions:

The first type:

Pass it directly as a parameter. For example
Copy the codeThe code is as follows:

<div onclick="show(this);">text</div>

<script type="text/javascript">
function show(obj){
();
}
</script>

The second type:

Use the hidden event object directly. The event object has a srcElement property that can be accessed directly
Copy the codeThe code is as follows:

<div onclick="show();">text</div>

<script type="text/javascript">
function show(){
();
}
</script>