在JS中以一种形式与“this”混淆

got confused with 'this' in one form in js

本文关键字:this 混淆 一种 JS      更新时间:2023-09-26
<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在这里代表什么?我以为是窗口对象,如果是这样,那么无法解释这行:onFocus="this.blur();",或者两者都this表示输入字段,如果是这样,如何用户站立this.form(input.form(?我对这里的'this'感到困惑,谁能给我解释一下?谢谢。

onFocus="getExpression(this.form(;" ,这在这里代表什么?

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

onFocus="this.blur((;">

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

this表示它指的是那个特定的形式,那个特定的元素。

示例:这是我发布过的最短答案(指这个问题(

这个关键字在JS环境中是一个棘手的关键字 我会推荐这篇文章,因为它确实帮助我前段时间理解了它。了解此关键字。

在这种情况下,如果函数不是由 call(( 或 apply(( 执行的,它修改了 this 关键字的行为,它应该表示函数的调用者,在这种情况下是触发事件的输入。

希望这对:)有所帮助