簡體   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