我想在javascript函数中添加一个按钮的背景色。这是我的密码。
$('#theme').on('click', function () {
myApp.modal({
verticalButtons: true,
buttons: [
{
text: 'Themes Category',
onClick: function() {
document.getElementById('content_01').style.display = "none";
document.getElementById('content_02').style.display = "block";
}
},
{
text: 'All themes',
onClick: function() {
myApp.alert('You clicked second button!')
}
},
]
})
});
<script type="text/javascript" src="js/my-app.js"></script>
<button class="col button button-fill" id="theme">Themes</button>
我添加了buttons.style.background = '#FF00AA';
以向函数中的按钮添加背景色。但不起作用。那我该怎么做呢。有人能帮我吗。
发布于 2018-06-18 22:55:05
给你一个解决办法
$('#theme').on('click', function () {
myApp.modal({
verticalButtons: true,
buttons: [{
text: 'Themes Category',
onClick: function() {
document.getElementById('content_01').style.display = "none";
document.getElementById('content_02').style.display = "block";
},
className: "buttonRed"
},
{
text: 'All themes',
onClick: function() {
myApp.alert('You clicked second button!')
},
className: "buttonRed"
}
]
})
});
.buttonRed {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="col button button-fill" id="theme">Themes</button>
您需要在CSS中定义类&使用className
属性将类添加到按钮中。
希望这能帮到你。
发布于 2018-06-18 22:50:47
使用this.style.backgroundColor="red";
或 in JQuery $(this).css("background-color","red");
见此处:css.asp
$('#theme').on('click', function () {
this.style.backgroundColor="red";
myApp.modal({
verticalButtons: true,
buttons: [
{
text: 'Themes Category',
onClick: function() {
document.getElementById('content_01').style.display = "none";
document.getElementById('content_02').style.display = "block";
}
},
{
text: 'All themes',
onClick: function() {
myApp.alert('You clicked second button!')
}
},
]
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="col button button-fill" id="theme">Themes</button>
发布于 2018-06-18 22:50:12
试试这个:
$(this).css('background-color','#f47121');
https://stackoverflow.com/questions/50922132
复制