javascript开闭window

javascript open and close window

我试图创建一个函数来打开和关闭规则 window 我不知道为什么但它不起作用,我试图搜索相同想法的示例但是我尝试了这是行不通的, 是的,如果有人知道,请发表评论

const openRulesBtn = document.getElementById("openRulesBtn");
const rulesWindow = document.getElementById("bgRules");
const closeRulesBtn = document.getElementById("rulesCancelBtn");

openRulesBtn.onclick = openRules();
closeRulesBtn.onclick = closeRules();

function openRules() {
  rulesWindow.style.display = "block";
}

function closeRules() {
  rulesWindow.style.display = "none";
}
.openRulesBtn {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 7rem;
  height: 1rem;
  margin: 2rem 4rem;
  border: 2px solid hsl(217, 16%, 45%);
  border-radius: 8px;
  text-transform: uppercase;
}

.openRulesBtn:hover {
  cursor: pointer;
}

#bgRules {
  position: absolute;
  top: 50%;
  left: 50%;
  display: none;
}

#rulesWindow {
  position: relative;
  z-index: 10;
  background-color: white;
}

#rulesCancelBtn {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 11;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid rgba(0, 0, 0, 0.3);
  cursor: pointer;
}

#rulesCancelBtn:hover {
  background-color: rgb(255, 30, 0);
}
<div id="bgRules">
  <img id="rulesWindow" src="/images/image-rules.svg" alt="" />
  <div id="rulesCancelBtn">x</div>
</div>
<footer>
  <div class="openRulesBtn" id="openRulesBtn">Rules</div>
</footer>

functionName() executes/calls 函数。要添加事件侦听器,请执行以下操作:

openRulesBtn.onclick = openRules;
closeRulesBtn.onclick = closeRules;

const openRulesBtn = document.getElementById("openRulesBtn");
const rulesWindow = document.getElementById("bgRules");
const closeRulesBtn = document.getElementById("rulesCancelBtn");

openRulesBtn.onclick = openRules;
closeRulesBtn.onclick = closeRules;

function openRules() {
    rulesWindow.style.display = "block";
}
function closeRules() {
    rulesWindow.style.display = "none";
}
.openRulesBtn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 7rem;
    height: 1rem;
    margin: 2rem 4rem;
    border: 2px solid hsl(217, 16%, 45%);
    border-radius: 8px;
    text-transform: uppercase;
}

.openRulesBtn:hover {
    cursor: pointer;
}

#bgRules {
    position: absolute;
    top: 50%;
    left: 50%;
    display: none;
}

#rulesWindow {
    position: relative;
    z-index: 10;
    background-color: white;
}

#rulesCancelBtn {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 11;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid rgba(0, 0, 0, 0.3);
    cursor: pointer;
}

#rulesCancelBtn:hover {
    background-color: rgb(255, 30, 0);
}
<div id="bgRules">
  <img id="rulesWindow" src="/images/image-rules.svg" alt="" />
  <div id="rulesCancelBtn">x</div>
</div>
<footer>
  <div class="openRulesBtn" id="openRulesBtn">Rules</div>
</footer>