HTMLDialogElement: show() 方法
HTMLDialogElement 介面的 show() 方法以非模態方式顯示對話方塊,即仍然允許與對話方塊外的其他內容進行互動。
語法
js
show()
引數
無。
返回值
無(undefined)。
異常
InvalidStateErrorDOMException-
如果對話方塊已開啟且為模態(即,如果對話方塊已透過
HTMLDialogElement.showModal()開啟),則會丟擲此錯誤。
示例
以下示例展示了一個簡單的按鈕,當點選該按鈕時,將透過 show() 方法開啟一個包含表單的 <dialog> 元素。然後,您可以點選取消按鈕(透過 HTMLDialogElement.close() 方法)關閉對話方塊,或透過提交按鈕提交表單。
html
<!-- Simple pop-up dialog box, containing a form -->
<dialog id="favDialog">
<form method="dialog">
<section>
<p>
<label for="favAnimal">Favorite animal:</label>
<select id="favAnimal" name="favAnimal">
<option></option>
<option>Brine shrimp</option>
<option>Red panda</option>
<option>Spider monkey</option>
</select>
</p>
</section>
<menu>
<button id="cancel" type="reset">Cancel</button>
<button type="submit">Confirm</button>
</menu>
</form>
</dialog>
<menu>
<button id="updateDetails">Update details</button>
</menu>
js
const updateButton = document.getElementById("updateDetails");
const cancelButton = document.getElementById("cancel");
const dialog = document.getElementById("favDialog");
dialog.returnValue = "favAnimal";
function openCheck(dialog) {
if (dialog.open) {
console.log("Dialog open");
} else {
console.log("Dialog closed");
}
}
// Update button opens a modeless dialog
updateButton.addEventListener("click", () => {
dialog.show();
openCheck(dialog);
});
// Form cancel button closes the dialog box
cancelButton.addEventListener("click", () => {
dialog.close("animalNotChosen");
openCheck(dialog);
});
規範
| 規範 |
|---|
| HTML # dom-dialog-show-dev |
瀏覽器相容性
載入中…
另見
- 實現此介面的 HTML 元素:
<dialog>。