我想从 Jquery 作用域(在同一个类中)调用同一个类的函数/方法。我该怎么办?
我试过:
displayTemplate.findWithAttr(action);
或
this.findWithAttr(action);
回应
"this.findWithAttr is not a function"
这是我的代码。
class displayTemplate{
findWithAttr(action) {
//to do something with action
}
router(action){
if(action == "something"){
$( "#confirm_dialog_msg" ).text("text?");
$( "#dialog-confirm" ).dialog({
buttons: {
"yes": function() {
$( this ).dialog( "close" );
//how in this area call to "findWithAttr" function above?
this.findWithAttr(action);
},
"No": function() {
//..
}
}
});
}
//...
}
}
最佳答案
在进入函数的 JQuery 范围之前,像这样声明一个变量
var self = this;
然后只需执行 self.findWithAttr
就可以了。
就像:
router(action){
if(action == "something"){
var self = this;
$( "#confirm_dialog_msg" ).text("text?");
$( "#dialog-confirm" ).dialog({
buttons: {
"yes": function() {
$( this ).dialog( "close" );
//how in this area call to "findWithAttr" function above?
self.findWithAttr(action);
},
"No": function() {
//..
}
}
});
}
希望这对您有所帮助。
关于javascript - 从 jquery 范围调用到类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56378714/