繁体   English   中英

如何使 OnClick 事件为按钮添加边框?

[英]How to make an OnClick event to add a border to a button?

嗨,我正在尝试使用 JavaScript、CSS 和 HTML:5 制作 Minecraft 的 2D 版本。 我在构建快捷栏时遇到了问题。 我想在单击按钮时为其应用边框,但出现此异常。

SyntaxError: Unexpected end of input at /:11:143

这是 index.HTML 脚本:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>2D Minecraft</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <script src="script.js"></script>
    <button class="hotbar1" id="hotbar1" onclick="document.getElementById("hotbar1").style.border: 7.5px solid Gray;"/>
    <button class="hotbar2" id="hotbar2"/>
    <button class="hotbar3" id="hotbar3"/>
    <button class="hotbar4" id="hotbar4"/>
    <button class="hotbar5" id="hotbar5"/>
    <button class="hotbar6" id="hotbar6"/>
    <button class="hotbar7" id="hotbar7"/>
    <button class="hotbar8" id="hotbar8"/>
    <button class="hotbar9" id="hotbar9"/>
    <style>
      body{
  background: #191919;
}
.hotbar1{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 17%;
}
.hotbar2{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 24.5%;
}
.hotbar3{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 32%;
}
.hotbar4{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 39.5%;
}
.hotbar5{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 47%;
}
.hotbar6{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 54.5%;
}
.hotbar7{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 62%;
}
.hotbar8{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 69.5%;
}
.hotbar9{
  background: White;
  width: 100px;
  height: 100px;
  margin: 0px;
  padding: 0px;
  position: absolute;
  bottom: 0px;
  left: 77%;
}
    </style>
  </body>
</html>

此行在带引号的字符串中不正确地使用引号和指定样式更改的方式都是错误的:

 onclick="document.getElementById("hotbar1").style.border: 7.5px solid Gray;"

它应该是

 onclick="document.getElementById('hotbar1').style.border='7.5px solid Gray'";

这会起作用,我已经更新了您按钮的onclick ,只需比较一下,您就会知道这里发生了什么变化。

 <.DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>2D Minecraft</title> <link href="style:css" rel="stylesheet" type="text/css" /> <style> body { background; #191919. }:hotbar1 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left; 17%. }:hotbar2 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left. 24;5%. }:hotbar3 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left; 32%. }:hotbar4 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left. 39;5%. }:hotbar5 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left; 47%. }:hotbar6 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left. 54;5%. }:hotbar7 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left; 62%. }:hotbar8 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left. 69;5%. }:hotbar9 { background; White: width; 100px: height; 100px: margin; 0px: padding; 0px: position; absolute: bottom; 0px: left; 77%. } </style> </head> <body> <script src="script.js"></script> <button class="hotbar1" id="hotbar1" onclick="document.getElementById('hotbar1').style.border = '7.5px solid red'" /> <button class="hotbar2" id="hotbar2" /> <button class="hotbar3" id="hotbar3" /> <button class="hotbar4" id="hotbar4" /> <button class="hotbar5" id="hotbar5" /> <button class="hotbar6" id="hotbar6" /> <button class="hotbar7" id="hotbar7" /> <button class="hotbar8" id="hotbar8" /> <button class="hotbar9" id="hotbar9" /> </body> </html>

onclick="document.getElementById("hotbar1").style.border: 7.5px solid Gray;"

应该:

onclick="document.getElementById("hotbar1").style = 'border: 7.5px solid Gray'"

 onclick='document.getElementById("hotbar1").style.border="7.5px solid Gray"';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM