javascript - 返回值到 DOM
问题描述
我正在寻找有关事件侦听器在 return 语句中返回值的指导。
我在网上找到了关于return语句的两条规则:
- 如果在函数体中使用 return 关键字,则将值返回(或发送)回调用函数的位置。
- 如果在事件处理程序中使用了 return 关键字,则会向浏览器返回(或发送)一个值。
我有以下 js 代码,用于应返回 true/false 的自定义确认模式。我希望它模仿浏览器的本机确认框。
在下面的 js 代码中,有一个包含事件处理程序的 switch 语句。事件处理程序应该运行该函数并返回 true,但我也不知道它返回到哪里或如何找到它。开发工具中是否有工具可以查看此值?
const dialog = {
open (options) {
options = Object.assign({}, {
title: '',
message: '',
type: 'I',
buttonGroup: 'okonly',
okText: 'OK',
cancelText: 'Cancel',
yesText: 'Yes',
npText: 'No',
onok: function () {return true;},
oncancel: function () {return false;},
onyes: function () {return true;},
onno: function () {return false;}
}, options);
const html = `
<div id="dds-dialogoverlay" class="dds-dialogoverlay">
<div id="dds-dialogbox" class="dds-dialogbox">
<div class="dds-dialogbox__head">
<span>Deployable Disbursing System (DDS) - ${options.title}</span>
<button id="dds-dialogbox__close" class="dds-dialogbox__close">×</button>
</div>
<div class="dds-dialogbox__body">
<div id="dds-dialogbox__body-left" class="dds-dialogbox__body-left"></div>
<div id="dds-dialogbox__body-right" class="dds-dialogbox__body-right">${options.message}</div>
</div>
<div id="dds-dialogbox__footer" class="dds-dialogbox__footer">
<div id="btn-groups">
<div id="okonly-btn-group" class="btn-group">
<button id="okonly_ok-btn" class="t-button" type="button" value="ok">${options.okText}</button>
</div>
<div id="okcancel-btn-group" class="btn-group">
<button id="okcancel_ok-btn" class="t-button" type="button" value="ok">${options.okText}</button>
<button id="okcancel_cancel-btn" class="t-button" type="button" value="cancel">${options.cancelText}</button>
</div>
<div id="yesno-btn-group" class="btn-group">
<button id="yesno_yes-btn" class="t-button" type="button" value="yes"${options.yesText}</button>
<button id="yesno_no-btn" class="t-button" type="button" value="no">${options.noText}</button>
</div>
<div id="yesnocancel-btn-group" class="btn-group">
<button id="yesnocancel_yes-btn" class="t-button" type="button" value="yes"${options.yesText}</button>
<button id="yesnocancel_no-btn" class="t-button" type="button" value="no">${options.noText}</button>
<button id="yesnocancel_cancel-btn" class="t-button" type="button" value="cancel">${options.cancelText}</button>
</div>
</div>
</div>
</div>
</div>`;
const template = document.createElement('template');
template.innerHTML = html;
// Elements
const dialogOverlay = template.content.querySelector('#dds-dialogoverlay')
const dialogbox = template.content.querySelector('#dds-dialogbox');
const dialogbox__head = template.content.querySelector('#dds-dialogbox__head');
const dialogbox__body = template.content.querySelector('#dds-dialogbox__body');
const dialogbox__footer = template.content.querySelector('#dds-dialogbox__footer');
const dialogbox__okonlyBtnGrp = template.content.querySelector('#okonly-btn-group');
const dialogbox__okcancelBtnGrp = template.content.querySelector('#okcancel-btn-group');
const dialogbox__yesnoBtnGrp = template.content.querySelector('#yesno-btn-group');
const dialogbox__yesnocancelBtnGrp = template.content.querySelector('#yesnocancel-btn-group');
const btnClose = template.content.querySelector('.dds-dialogbox__close');
const btnOk = template.content.querySelector('#okonly_ok-btn');
const btnOkCancel_ok = template.content.querySelector('#okcancel_ok-btn');
const btnOkCancel_cancel = template.content.querySelector('#okcancel_cancel-btn');
const btnYesNo_yes = template.content.querySelector('#yesno_yes-btn');
const btnYesNo_no = template.content.querySelector('#yesno_no-btn');
const btnYesNoCancel_yes = template.content.querySelector('#yesnocancel_yes-btn');
const btnYesNoCancel_no = template.content.querySelector('#yesnocancel_no-btn');
const btnYesNoCancel_cancel = template.content.querySelector('#yesnocancel_cancel-btn');
var dialog_type_class;
switch (options.type) {
case 'I':
dialog_type_class = 'dds-dialogbox--info';
break;
case 'E':
dialog_type_class = 'dds-dialogbox--error';
break;
case 'W':
dialog_type_class = 'dds-dialogbox--warning';
case 'S':
dialog_type_class = 'dds-dialogbox--success';
}
dialogOverlay.classList.add('dds-dialogoverlay--show');
dialogbox.classList.add(dialog_type_class);
switch (options.buttonGroup) {
case 'okonly':
dialogbox__okonlyBtnGrp.classList.add("btn-group--show");
btnOk.addEventListener('click', () => {
response = options.onok();
this._close(dialogbox, dialogOverlay);
console.log('response is ' + response);
return response;
});
break;
case 'okcancel':
dialogbox__okcancelBtnGrp.classList.add("btn-group--show");
btnOkCancel_ok.addEventListener('click', () => {
response = options.onok();
this._close(dialogbox, dialogOverlay, response);
});
btnOkCancel_cancel.addEventListener('click', () => {
response = options.oncancel();
this._close(dialogbox, dialogOverlay, response);
});
break;
case 'yesno':
dialogbox__yesnoBtnGrp.classList.add("btn-group--show");
btnYesNoCancel_yes.addEventListener('click', () => {
response = options.onyes();
this._close(dialogbox, dialogOverlay, response);
});
btnYesNo_no.addEventListener('click', () => {
response = options.onno();
this._close(dialogbox, dialogOverlay, response);
});
break;
case 'yesnocancel':
dialogbox__yesnocancelBtnGrp.classList.add("btn-group--show");
btnYesNoCancel_yes.addEventListener('click', () => {
response = options.onyes();
this._close(dialogbox, dialogOverlay, response);
});
btnYesNoCancel_no.addEventListener('click', () => {
response = options.onno();
this._close(dialogbox, dialogOverlay, response);
});
btnYesNoCancel_cancel.addEventListener('click', () => {
response = options.oncancel();
this._close(dialogbox, dialogOverlay, response);
});
}
dialogOverlay.addEventListener('click', e => {
if (e.target === dialogOverlay) {
options.oncancel();
this._close(dialogbox, dialogOverlay);
}
})
dialogbox.addEventListener('click', e => {
if (e.target === dialogbox) {
options.oncancel();
this._close(dialogbox, dialogOverlay);
}
});
document.body.appendChild(template.content);
},
_close (dialogbox, dialogOverlay) {
dialogbox.classList.remove(dialogbox.classList.item(1));
dialogOverlay.classList.remove("dds-dialogoverlay--show");
document.body.removeChild(dialogOverlay);
}
};
function Dialogbox(dialog_type = 'I', dialog_title, dialog_mess, dialog_buttons = 'okonly') {
dialog.open({
title: dialog_title,
message: dialog_mess,
type: dialog_type,
buttonGroup: dialog_buttons
});
}
解决方案
EventListener 的回调不会向浏览器返回任何内容。如果您在事件处理程序中有 return 语句,它只会在那里停止执行事件处理程序。有关更多详细信息,您可以通过 URL:https ://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#The_event_listener_callback
推荐阅读
- debugging - 从回溯完成帧局部变量
- php - $table->stamps() 记录在哪里以及如何回滚?
- javascript - 是否可以为 graphql-yoga 输出 JSON 格式的日志?
- azure - Istio 为虚拟服务中映射的 URL 抛出 404
- r - 如何在 R 中的列的开头插入“1”?
- python-3.x - 如何调试此 AttributeError:'int' 对象没有属性 'increase'?
- python - 如何使用 openpyxl 在 Excel 工作表中插入数组公式?
- unit-testing - 在 Spock 中使用模拟单元测试流畅的 API
- kubernetes - 是否可以将新 pod 动态添加到现有部署中?
- javascript - Nightwatch 变量范围和访问外部 .element 调用