当前位置: 首页 > 知识库问答 >
问题:

单击“随机数”并更改颜色?

严书
2023-03-14
class diceButton {
  constructor(colsElement) {
    this.containerElement = colsElement;
    this.dice = document.createElement('button');
    this.dice.type = 'button';
    this.dice.className = 'btn btn-lg';
  }
  setDiceStyle() {
    let n = 1;
  }
  randomNumber() {
    return Math.floor(Math.random() * 6) + 1;
  }
  onChangeState() {}
}
button {
  margin-top: 100px;
  width: 250px;
  height: 250px;
}
<div class="container">
  <div class="row justify-content-center">
    <div class="cols" id="casino">
      <button type="button" id='randomBTN'>Click Me!</button>
    </div>
  </div>
</div>

我想创建一个按钮,随机数字,按钮颜色如下引导CSS类:

1=主要,2=次要,3=成功,4=危险,5=警告,6=信息(背景颜色)在屏幕或刷新的开始,您必须始终随机化一个数字,并为按钮分配颜色和值。点击按钮就是总是随机化一个新数字,并改变它的颜色和值。

共有3个答案

东和怡
2023-03-14

下面是一个自举的解决方案。您可以在css中创建自己的btn-...类,然后使用它们。这样你就不需要引导,可以摆脱链接标签。例如:. btn-危险{背景颜色:红色}

class diceButton {
  constructor(colsElement) {
    this.classes = ["primary", "secondary", "success", "danger", "warning", "info"]
    this.containerElement = colsElement;
    this.dice = this.create();
    var that = this;
    this.dice.addEventListener("click", function() {
      that.setDiceStyle.apply(that, []);
    })
    this.dice.type = 'button';
    this.dice.className = 'btn btn-lg';
    this.setDiceStyle()
  }
  create() {
    let e = document.createElement("button");
    e.setAttribute("type", "button");
    e.innerText = "Click Me!";
    this.containerElement.appendChild(e);
    return e;
  }
  setDiceStyle() {
    let n = this.randomNumber();
    this.dice.setAttribute("class", "btn btn-" + this.classes[n]);
  }
  randomNumber() {
    return Math.floor(Math.random() * 6);
  }
}

var d = new diceButton(document.getElementById("casino"));
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">


<div class="container">
  <div class="row justify-content-center">
    <div class="cols" id="casino">
    </div>
  </div>
</div>
卫烨烁
2023-03-14

如果您只想更改当前按钮,请尝试以下操作

const bsColors = ["btn-primary","btn-secondary","btn-success","btn-danger","btn-warning","btn-info"];
const randomNumber = len => { return Math.floor(Math.random() * len) };


class diceButton {
  constructor(colsElement) {
    this.containerElement = colsElement;
    this.dice = document.createElement('button');
    this.dice.id = 'randomBTN';
    this.dice.type = 'button';
    const type = bsColors[randomNumber(bsColors.length)]; // or fill out setDiceStyle
    this.dice.className = 'btn btn-lg '+type;
    this.dice.innerText = type;
    this.dice.addEventListener("click",this.onChangeState)
    this.containerElement.appendChild(this.dice);
  }
  setDiceStyle() {
    let n = 1;
  }
  onChangeState() { 
    // console.log(this.innerText) 
    this.classList.forEach(clas => {
      if (clas.startsWith('btn-')) { 
        this.classList.remove(clas);
        const newType = bsColors[randomNumber(bsColors.length)]
        this.classList.add(newType);
        this.innerText = newType;
      }
    })  
  }
}
const container = document.getElementById("casino");
const btn = new diceButton(container);
button {
  margin-top: 100px;
  width: 250px;
  height: 250px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />

<div class="container">
  <div class="row justify-content-center">
    <div class="cols" id="casino"></div>
  </div>
</div>
彭弘伟
2023-03-14

这是你想要的例子,它会在加载时分配一个随机类,然后它会随着每次点击而改变。它不使用你的方法,但它的工作是预期的。

var classArray = ['btn-primary','btn-secondary','btn-success','btn-danger','btn-warning','btn-info'];
  var randomNumber = Math.floor(Math.random() * classArray.length);
  var btn = document.getElementById('randomBTN');
  btn.classList.add(classArray[randomNumber]);

  btn.onclick = function(){
    var randomNumber2 = Math.floor(Math.random() * classArray.length);
    this.classList.forEach(className => {
        if (className.startsWith('btn-')) {
            this.classList.remove(className);
        }
    });
    btn.classList.add(classArray[randomNumber2]);
    
    btn.innerHTML = "<h5>" + classArray[randomNumber2] + "<br><br>" + randomNumber2 + "</h5>"
}
button {
  margin-top: 10px;
  width: 250px;
  height: 250px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
  <div class="row justify-content-center">
    <div class="cols" id="casino">
      <button type="button" id='randomBTN' class="btn">Click Me!</button>
    </div>
  </div>
</div>
 类似资料:
  • 如何更改DataTable中单击行的颜色? 我有我的数据表和一个JavaScript来对单击行的数据执行一些操作: 谢谢

  • 如果你点击按钮,你会看到一个蓝色的边框,这对我来说真的很烦。对于如何让它消失/根本不出现,你有什么解决方案吗?

  • 我有一个简单的问题。 单击搜索图标时,如何将工具栏的颜色/或主题更改为白色? 每当单击搜索图标时,我希望工具栏为白色(向后箭头为黑色/灰色)。我在MenuItem中添加了SearcView(android.support.v7.widget.SearchView) 到 然后回到原来的工具栏颜色时,单击返回按钮。

  • 我是javascript新手,这可能是一个简单的问题。。。 我在这里所做的是修改div(#box)的css样式,这取决于你所在页面的位置( 我试图实现的是向#box添加多个css更改,并每次执行一个随机更改。例如,每次从最顶端滚动通过200,div的显示高度为:绿色背景为“100px”,蓝色背景为“200px”,或红色背景为“300px”。希望这有意义。。。。

  • 我有一个简单的部分,用户可以点击一个按钮,现在我想要点击改变(切换)文本的颜色,使用反应钩,这是我目前所拥有的。 因此,单击时初始颜色为黑色,我正在将颜色更改为。现在,再次单击时,颜色不会改变。 我需要添加什么,使这个切换在这两种颜色之间单击?

  • 所以我从昨天开始就一直在使用Javascript(Vue)和html(所以请原谅我的困惑),我不知道如何在更改链接的同时更改“home”链接描述旁边的图像的颜色。 我试过使用CSS类,它工作,但只改变图像颜色时,你实际上悬停在图像上,而不是整个链接线。除了CSS之外,我也尝试过通过“onmouseover”将图像改变为彩色版本,但不知怎么的,这将图标立即改变为彩色版本(?)。我猜问题是我通过一个: