我一直在研究一个项目中的对话框,并将它变成一个对象构造函数。
function createDialog(title, noClicked = function(){}, yesClicked = function(){}) {
this.dialog = document.getElementsByClassName("dialog")[0];
this.dialogTitle = this.dialog.getElementsByTagName("h1")[0];
this.No = this.dialog.getElementsByTagName("button")[0];
this.Yes = this.dialog.getElementsByTagName("button")[1];
var dialog = document.getElementsByClassName("dialog")[0];
this.dialogTitle.innerHTML = title;
document.getElementsByClassName("dialogCon")[0].style.display = "flex";
noClicked();
yesClicked();
}
<div class="dialogCon">
<div class="dialog">
<h1></h1>
<button type="button">No</button>
<button type="button">Yes</button>
</div>
</div>
问题是,当我想访问“this.no”或“this.yes”时,我不断收到Cannot read property 'No' of undefined。当我使用以下代码时发生了这种情况:
var d = new createDialog("Sample dialog. Exit?", function() {
console.log(d.No);
}, function() {
console.log(d.Yes);
});
我需要使用 d.No 关闭对话框。还有其他方法吗?或者至少是一个修复。
我知道我可以从构造函数本身关闭对话框,但我也想让它也可以做其他事情(比如检测用户是否选择是或否)。
catspeake
吃鸡游戏
相关分类