为什么我的 click() 到 on() 转换没有按预期工作?
jquery 中的 .click 已被弃用。所以我对现有的代码进行了修改,但是,似乎存在一些问题。
GetPageButtons() {
let $saveButton = $("<input />", {
type: "button",
value: "Save",
class: "button large save",
}),
$cancelButton = $("<input />", {
type: "button",
value: "Cancel",
class: "button large cancel",
});
$saveButton.click(() => {
this.UpdateRecord();
});
$cancelButton.click(() => {
this.modal.success = true;
let _parent = this;
$.each(_parent.fields, function (i, field) {
delete this.newValue;
this.element.val("");
});
});
return [$cancelButton, $saveButton];
}
上面的代码是有效的,尽管我的 linter 抱怨说它已被弃用。
GetPageButtons() {
let $saveButton = $("<input />", {
type: "button",
value: "Save",
class: "button large save",
}),
$cancelButton = $("<input />", {
type: "button",
value: "Cancel",
class: "button large cancel",
});
$saveButton.on("click", function() {
this.UpdateRecord();
});
$cancelButton.on("click", function() {
this.modal.success = true;
let _parent = this;
$.each(_parent.fields, function (i, field) {
delete this.newValue;
$( this ).element.val("");
});
});
return [$cancelButton, $saveButton];
}
然而,对建议使用 .on('click', fn){} 的重构不起作用。我收到一条错误消息:
“未捕获的类型错误:this.UpdateRecord 不是函数”。
为什么?它的工作方式应该与 .click 相同,.click 是 .on 的简写。 有什么建议吗???
The .click in jquery is deprecated. So I made amendments to the existing code, however, there seems to be some issue.
GetPageButtons() {
let $saveButton = $("<input />", {
type: "button",
value: "Save",
class: "button large save",
}),
$cancelButton = $("<input />", {
type: "button",
value: "Cancel",
class: "button large cancel",
});
$saveButton.click(() => {
this.UpdateRecord();
});
$cancelButton.click(() => {
this.modal.success = true;
let _parent = this;
$.each(_parent.fields, function (i, field) {
delete this.newValue;
this.element.val("");
});
});
return [$cancelButton, $saveButton];
}
The above code works, although my linter complains saying it deprecated.
GetPageButtons() {
let $saveButton = $("<input />", {
type: "button",
value: "Save",
class: "button large save",
}),
$cancelButton = $("<input />", {
type: "button",
value: "Cancel",
class: "button large cancel",
});
$saveButton.on("click", function() {
this.UpdateRecord();
});
$cancelButton.on("click", function() {
this.modal.success = true;
let _parent = this;
$.each(_parent.fields, function (i, field) {
delete this.newValue;
$( this ).element.val("");
});
});
return [$cancelButton, $saveButton];
}
The refactor to the suggested use of .on('click', fn){} however is not functioning. I get an error that says:
"Uncaught TypeError: this.UpdateRecord is not a function".
Why? It should work the same as the .click which is shorthand for .on.
Any suggestions???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是行不通的,因为您使用的是普通函数,而不是使用箭头函数的原始代码。
这意味着函数内的
this
指的是不同的东西。将其更改为
,它应该像原来一样工作。
This does not work, because you are using normal function in contrast to the original code that used arrow function.
This means that the
this
inside the functions refers to something different.Change it to
and it should work as originally.