函数参数不能与javascript中的window.onblur和onfocus一起使用

问题描述:

以下代码无法使用



代码:

The following code isn't working

Code:

window.onfocus = welcome("John Doe");
window.onblur = bye("John Doe");

function welcome(name) {
    $("p").append("Welcome back " +name+ " <br>");
}

function bye(name) {
    $("p").append("Good bye " +name+ " see you soon <br>");
}



但是我注意到这段代码适用于不带任何参数的自定义函数。见下面的代码。以下代码正常运行:



代码:


However I noticed that this code works for custom functions that dont take any argument. See code below. The following code is working:

Code:

window.onfocus = welcome; //No Argument here so this code works
window.onblur = bye; //Same here... No argument for the function

function welcome(name) {
    $("p").append("Welcome back John Doe <br>");
}

function bye(name) {
    $("p").append("Good bye John Doe see you soon <br>");
}



我是jquery和javascript的新手,这些东西让我感到困惑。有人可以解释为什么它不起作用。如何使用window.onfocus和onblur方法创建带参数的自定义函数。 (工作或不工作的原因对我来说更重要,因为我想了解机制而不是填写片段)



我尝试过:




I am new to jquery and javascript and this stuff is confusing me. Can someone please explain why exactly it isn't working. and how can I make a custom function with arguments work with window.onfocus and onblur method. (reason for working or not working is more important for me as I want to understand the mechanics rather than cramming up the snippets)

What I have tried:

window.onfocus = welcome("John Doe");
window.onblur = bye("John Doe");

function welcome(name) {
    $("p").append("Welcome back " +name+ " <br>");
}

function bye(name) {
    $("p").append("Good bye " +name+ " see you soon <br>");
}

p)。append( 欢迎回来 + name + < br>);
}

function 再见(名称){
("p").append("Welcome back " +name+ " <br>"); } function bye(name) {


p)。append( 再见 +名称+ 很快见到你< br>);
}
("p").append("Good bye " +name+ " see you soon <br>"); }



但是我注意到这段代码适用于不带任何参数的自定义函数。见下面的代码。以下代码正常运行:



代码:


However I noticed that this code works for custom functions that dont take any argument. See code below. The following code is working:

Code:

window.onfocus = welcome; //No Argument here so this code works
window.onblur = bye; //Same here... No argument for the function

function welcome(name) {


p)。append( 欢迎回来John Doe< br>);
}

function 再见(姓名){
("p").append("Welcome back John Doe <br>"); } function bye(name) {