繁体   English   中英

我无法理解为什么在myFunction(this)和myFunction(x)中使用它们? 烦扰我的心灵

[英]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>

我无法理解为什么在myFunction(this)和myFunction(x)中使用它们? 烦扰我的心灵

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

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

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

意味着在这种情况下,x = this和this =现在焦点位于的输入,因此x =执行功能时的焦点输入。 本质上,该函数将执行input.style.background =“ Red”(将输入背景设置为红色)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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