HTML中的dialog元素


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/12/09/HTML%E4%B8%AD%E7%9A%84dialog%E5%85%83%E7%B4%A0/

摘要

本文主要讲述了:

  1. 语义
  2. attribute
  3. property
  4. method
  5. event
  6. polyfill

正文

语义

对话框

示例:

learn_dialog/index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link rel="stylesheet" href="./css/index.css" />
<script defer src="./js/index.js"></script>
</head>
<body>
<button id="show">Show</button>
<button id="showModal">ShowModal</button>
<dialog id="foo">
<h1>hello, world</h1>
<form method="dialog">
<button type="submit" value="bar">Close</button>
</form>
</dialog>
</body>
</html>

learn_dialog/css/index.css

1
2
3
4
5
6
7
#foo {
background-color: #0f0;
}

#foo::backdrop {
background-color: #f00;
}

learn_dialog/js/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
const show = document.querySelector('#show');
const showModal = document.querySelector('#showModal');
const foo = document.querySelector('#foo');

show.addEventListener('click', function () {
foo.show();
});
showModal.addEventListener('click', function () {
foo.showModal();
});
foo.addEventListener('close', function (event) {
console.log(this.returnValue);
});

attribute

open

可见状态

property

open

可见状态

returnValue

用户点击<button>value

method

show

显示<dialog>

showModal

以模态窗口的形式显示<dialog>,此时<dialog>会和::backdrop伪元素一起,显示在其他任何可能出现的对话框的上方

close

关闭<dialog>

event

close

关闭事件

polyfill

截至目前,除了 Chrome 之外的其他浏览器对于<dialog>的支持或多或少地存在问题,此时可以引入 polyfill 实现跨浏览器兼容

dialog-polyfill

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/12/09/HTML%E4%B8%AD%E7%9A%84dialog%E5%85%83%E7%B4%A0/


本文对你有帮助?请支持我


支付宝
微信