使用css html的彩色按钮

mwg9r5ms  于 2022-12-27  发布在  其他
关注(0)|答案(4)|浏览(119)

我有代码html

<button onclick=location=URL>Refresh</button>

代码是如何被赋予css颜色?2例如
CSS代码'

.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 8px 8px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

.button2 {background-color: #008CBA;} /* Blue */

'在html中

<button class="button button2">Refresh</button>

但如果在此代码中实现,

<button onclick=location=URL>Refresh</button>

发生错误
有谁能帮我一下吗?

llmtgqce

llmtgqce1#

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .button {
        background-color: #4CAF50;
        /* Green */
        border: none;
        color: white;
        padding: 8px 8px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 4px 2px;
        cursor: pointer;
    }

    .button2 {
        background-color: #008CBA;
    }

    /* Blue */
</style>

<body>
    <button class="button button2"
        onclick="window.location.href='https://stackoverflow.com/questions/74922452/color-button-using-css-html';">Refresh</button>
</body>

</html>````
omtl5h9j

omtl5h9j2#

onclick已经过时并且很难阅读。请考虑对您的按钮使用侦听器!
刷新页面的代码为window.location.reload();

let btn_01;
    document.addEventListener("DOMContentLoaded", onReady);
    function onReady(){
        btn_01 = document.getElementById("button_1");
        btn_01.addEventListener("click",click_1);
    }
    function click_1(){
        window.location.reload();
    }
.button {
  background-color: #4CAF50; /* Green */
  border: none;
  color: white;
  padding: 8px 8px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}

.button2 {background-color: #008CBA;} /* Blue */
<button id="button_1" class="button button2">Refresh</button>
nx7onnlm

nx7onnlm3#

你必须改变两件事:

  • 点击属性:你必须有这个:"周围的javascript。喜欢
<button onclick="location=URL" >Refresh</button>
  • 如果您喜欢将样式与.button一起使用,则必须添加属性class="button" .如下所示:
<button onclick="location=URL" class="button" >Refresh</button>

**或者,**您可以使用其他思考方式:使用button{ ... }代替.button{ ... }

t9aqgxwy

t9aqgxwy4#

我看不出您是如何使用onclick的。但这里有问题:

<button onclick=location=URL>Refresh</button>

括号不见了。也许可以试试类似的东西

<button onclick="location.href='../'">

相关问题