打开/关闭<div>
对话框时出现问题。我的代码如下:
<html>
<head>
<title>Wochenplaner</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="dialog" style="display: none;">
<p id="demo">test</p>
<button onclick="close()">Close</button>
</div>
<button onclick="open()">Click me</button>
<script>
var test = document.getElementById("dialog");
function dialog() {
if (test.style.display !== "none") {
test.style.display = "none";
} else {
test.style.display = '';
}
}
function open() {
test.style.display = '';
}
function close() {
test.style.display = "none";
}
</script>
</body>
</html>
当我点击"点击我"时,按钮消失了,其他什么都没有发生。如果我使用dialog()-Function而不是open()和close(),它会按预期工作,单击按钮"click me"会出现<div>
,单击"close"或"click me"会消失。我的问题是:为什么我使用dialog(),而不使用open()和close()?
问题在于函数open()的名称。改变它,它就会工作:)
Open是JavaScript 中的保留词