是什么导致我的 javascript 函数无法运行?

问题描述 投票:0回答:1

我正在尝试用html和js制作一个简单的装扮游戏,但我不知道为什么该功能不起作用。正在记录窗口 onload 事件,但 Makeupclass1 函数没有执行任何操作。很抱歉,如果这是一个非常愚蠢的错误,我才刚刚开始学习。这是代码:

window.onload = (event) => {
  console.log("page is fully loaded");
};

function makeupclass1() {
  document.getElementById('makeup').setAttribute("class", "makeup1");
  console.log("makeup changed");
}
#makeup {
  width: 720px;
  height: 1280px;
  background-size: cover;
  position: absolute;
}

.makeup1 {
  background-image: url('img/makeup1.png');
}
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>

  <div id="makeup"></div>

  <button onclick="makeupclass1()">option 1</button>
</body>
</html>

我已经通过几个验证器运行了代码,但没有任何结果。我尝试过使用 className 来代替,并且我尝试过直接在 onclick 中使用该函数。

谢谢

javascript html onclick setattribute
1个回答
0
投票

#makup 覆盖了按钮,所以添加

pointer-events:none
到它:

window.onload = (event) => {
  console.log("page is fully loaded");
};

function makeupclass1() {
  document.getElementById('makeup').setAttribute("class", "makeup1");
  console.log("makeup changed");
}
#makeup {
  width: 720px;
  height: 1280px;
  background-size: cover;
  position: absolute;
  pointer-events:none;
}

.makeup1 {
  background-image: url('img/makeup1.png');
}
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>

  <div id="makeup"></div>

  <button onclick="makeupclass1()">option 1</button>
</body>
</html>

© www.soinside.com 2019 - 2024. All rights reserved.