简体   繁体   中英

I am unable to understand why they used at myFunction(this) and myFunction(x)? Bugging My Mind

<!DOCTYPE html>
<html>
<head>
    <title>Onfocusin</title>
</head>
<body>
    Enter Your Name:
    <input type="text" onfocusin="myFunction(this)">
    <script>
        function myFunction(x) {
            x.style.background = "Red";
        }
    </script>
</body>
</html>

I am unable to understand why they used at myFunction(this) and myFunction(x)? Bugging My Mind

this refers to the current element , in your case input element is passed in the function. Then it`s style is changed using the function myFunction(x). This is an example of pass by reference.

myFunction(x) is the declaration of the function where x is the parameter that is passed in. In this case that will be an input.

myFunction(this) is calling the function so this states that onfocusin event for the input it will call myFunction using the input that is now in focus as the parameter.

Meaning that in this case x = this and this = the input that focus is now on therefore x = focused input when executing the function. In essence the function will be doing input.style.background = "Red" (setting the input background to red)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM