gpt4 book ai didi

javascript - 与 js 中的一种形式的 'this' 混淆

转载 作者:行者123 更新时间:2023-11-28 16:00:02 25 4
gpt4 key购买 nike

<HTML>
<HEAD>
<TITLE>Example 5.3</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function calculate(form) {
form.results.value = eval(form.entry.value);
}
function getExpression(form) {
form.entry.blur();
form.entry.value = prompt("Please enter a JavaScript mathematical expression","");
calculate(form);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM METHOD=POST>
Enter a JavaScript mathematical expression:
<INPUT TYPE=text NAME="entry" VALUE="" onFocus="getExpression(this.form);">
<BR>
The result of this expression is:
<INPUT TYPE=text NAME="results" VALUE="" onFocus="this.blur();">
</FORM>
</BODY>
</HTML>

以上代码来自一个js教程。

问题:

onFocus="getExpression(this.form);" ,这里的 this 代表什么?我以为是window对象,如果是这样,那么就无法解释这一行:onFocus="this.blur();",或者两者都表示this表示输入字段,如果是这样,如何使用 this.form(input.form)?我在这里对 'this' 感到困惑,有人能给我解释一下吗?谢谢。

最佳答案

onFocus="getExpression(this.form);" , what does this represent here?

this这是您的输入 <INPUT TYPE=text NAME="entry"/>

onFocus="this.blur();"

this这是您的输入 <INPUT TYPE=text NAME="results"/>

关于javascript - 与 js 中的一种形式的 'this' 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17290062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com