我不明白为什么他们在myFunction(this)和myFunction(x)中使用?纠缠着我的脑海

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

本文关键字:myFunction 纠缠 脑海 我的 明白 他们 this 为什么      更新时间:2023-09-26
<!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>

我不明白为什么他们在myFunction(this(和myFunction(x(中使用?纠缠我的思想

this是指当前元素,在您的情况下,输入元素在函数中传递。然后它的样式使用函数 myFunction(x( 更改。这是按引用传递的示例。

myFunction(x( 是函数的声明,其中 x 是传入的参数。在这种情况下,这将是一个输入。

myFunction(this(正在调用该函数,因此它声明输入的onfocusin事件将使用现在处于焦点中的输入作为参数来调用myFunction。

这意味着在这种情况下 x = 这个

和这个 = 现在关注的输入,因此 x = 执行函数时的焦点输入。本质上,该函数将执行input.style.background = "Red"(将输入背景设置为红色(