2015-12-15 58 views
0

我不得不打开/关闭<div>对话的问题。我的代码如下所示:打开并通过Javascript功能来关闭对话框元素

<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> 

当我点击“点击我”按钮消失,并没有什么事情发生。如果我使用对话框() - 函数,而不是打开()和close(),它按预期工作时,<div>出现与在按钮上点击“点击我”和“关闭”或“点击我”与一个点击消失。 我的问题是:为什么当我使用dialog(),但不打开()和close()?

回答

6

的问题是你的函数打开()的名称。改变它,它会工作:)

Open是JavaScript中的保留字

+2

在那个笔记上,函数名应该更直观。 'openDialog()' – isherwood