我是materialize.css的新手,实际上我想用javascript在点击时改变按钮的颜色。每次点击按钮时,都必须根据按钮内容产生不同的颜色。但是当我点击按钮时,它不会改变颜色。
<div class=" buttons row center-align">
<button class="btn col s12 m6 deep-purple darken-4 center-align" id='button0' onclick ="answer('0')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option0"></span></button><br>
<button class="btn col s12 m6 deep-purple darken-4 center-align" id='button1' onclick ="answer('1')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option1"></span></button><br>
<button class="btn col s12 m6 deep-purple darken-4 center-align" id='button2' onclick ="answer('2')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option2"></span></button><br>
<button class="btn col s12 m6 deep-purple darken-4 center-align" id='button3' onclick ="answer('3')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option3"></span></button><br>
</div>javascript
function answer(ans){
var z = document.getElementById('option'+ans);
var choice = z.innerHTML;
var b = document.getElementById('button'+ans);
if(choice == questions[x].answer){
b.style.backgroundColor = '#008000';
b.style.color = 'rgb(255, 255, 255)';}
else{
b.style.backgroundColor = '#700000';
b.style.color = 'rgb(255, 255, 255)';}发布于 2018-04-06 07:14:00
html
<div class=" buttons row center-align">
<button class="button col s12 m6 center-align" id='button0' onclick ="answer('0')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option0"></span></button><br>
<button class="button col s12 m6 center-align" id='button1' onclick ="answer('1')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option1"></span></button><br>
<button class="button col s12 m6 center-align" id='button2' onclick ="answer('2')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option2"></span></button><br>
<button class="button col s12 m6 center-align" id='button3' onclick ="answer('3')" style ="display:none"><span class=" flow-text white-text text-darken-2" id="option3"></span></button><br>
只需删除materializecss按钮类'btn‘并替换为任何名称,我对button执行了操作,并删除了button标记中的materializecss颜色类,所以最后每个按钮都属于class:
'button col s12 m6 center-align' 并在此html文件的自创建css文件中(main.css)
.button.col.s12.m6.center-align{
background-color: #311b92;
padding: 10px 20px 10px;
border-radius: 50px;
font-size: 2em;}现在,当我们应用上述问题中提到的javascript方法时,按钮的颜色将在单击时发生变化
https://stackoverflow.com/questions/49681404
复制相似问题