[英]Explain why function won't execute when button clicked
I can't get the button within the signIn function to execute on the onclick event.我无法让 signIn 函数中的按钮在 onclick 事件上执行。 I need help understanding what I am doing wrong and what needs to be done to fix it.
我需要帮助了解我做错了什么以及需要做些什么来解决它。 I am building a simple silly app to learn javascript/typescript.
我正在构建一个简单的愚蠢应用程序来学习 javascript/typescript。 The other buttons earlier in the app work and I am able to transform the DOM but the button built within the signin function doesn't seem to fire and execute the isValid function.
应用程序中早期的其他按钮工作,我能够转换 DOM,但登录函数中内置的按钮似乎没有触发并执行 isValid 函数。 I have no errors within the web console.
我在网络控制台中没有错误。 I am completely stumped as to why the button will not execute the isValid function.
我完全不明白为什么按钮不会执行 isValid 函数。
var App = /** @class */ (function () { function App() { this.init(); } App.prototype.init = function () { if (!window['WebSocket']) { console.log("BROWSER NOT SUPPORTED"); document.body.innerHTML = "BROWSER NOT SUPPORTED"; } document.documentElement.style.background = "green"; document.title = "App"; var sin = document.createElement("button"); sin.id = "sin"; sin.innerHTML = "SIGN IN"; sin.onclick = this.signIn; var sup = document.createElement("button"); sup.id = "signup"; sup.innerHTML = "SIGN UP"; sup.onclick = this.signUp; var landing = document.createElement("div"); landing.id = "landing"; landing.style.position = "absolute"; landing.style.left = "50%"; landing.style.top = "50%"; landing.style.transform = "translate(-50%,-50%)"; landing.appendChild(sin); landing.appendChild(sup); document.body.appendChild(landing); }; App.prototype.signIn = function () { document.body.removeChild(document.getElementById("landing")); var user = document.createElement("input"); user.type = "text"; user.id = "username"; user.placeholder = " username"; user.style.border = "1px solid"; user.style.width = "150px"; var pass = document.createElement("input"); pass.type = "password"; pass.id = "password"; pass.placeholder = " password"; pass.style.border = "1px solid"; pass.style.width = "150px"; var okay = document.createElement("button"); okay.innerHTML = "Sign in"; //I can't get the isValid function to execute. okay.onclick = this.isValid; var signin = document.createElement("div"); signin.id = "signin"; signin.style.position = "absolute"; signin.style.left = "50%"; signin.style.top = "50%"; signin.style.transform = "translate(-50%,-50%)"; signin.appendChild(user); signin.appendChild(document.createElement("br")); signin.appendChild(pass); signin.appendChild(document.createElement("br")); signin.appendChild(okay); document.body.appendChild(signin); }; App.prototype.signUp = function () { alert("alert!"); }; App.prototype.isValid = function () { alert("alert!"); }; return App; }()); window.onload = function () { var app = new App(); };
This is a classic scoping problem.这是一个经典的范围界定问题。
When the okay
button is added, this
is not pointing where you thinking it is pointing.添加
okay
按钮后, this
不是指向您认为它指向的位置。
You assumed this
would be referring to the App
function.您认为
this
指的是App
函数。 But the okay
button is not created by the App
function, is it created by the sin
button.但是
okay
按钮不是App
函数创建的,是sin
按钮创建的。 And that's why this
, in that location, points to the sin
button, and since there no isValid
function in the sin
button, you don't get the alert you wanted.这就是为什么
this
,在该位置,指向sin
按钮,因为没有isValid
在功能sin
按钮,你没有得到警告你想要的。
To fix this you can change this.isValid
to App.prototype.isValid
.要解决此问题,您可以将
this.isValid
更改为App.prototype.isValid
。
// from this
okay.onclick = this.isValid;
// to this
okay.onclick = App.prototype.isValid;
This will get the desired behavior.这将获得所需的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.