我是Javascript的新手,并且一直在尝试在我的ASP.NET MVC项目中使用Javascript。
我有一个onclick更改的按钮,例如从ON到OFF。这些按钮有很多 - 每个按钮一个。 我想知道一个按钮在按下时会发生什么变化,即它变为OFF还是ON?
目前,我使用if语句来检查toggleClass,但是,我不相信这是当前的方法,并且声明在需要时不会产生True。
以下是一些示例代码:
<a id="menu-toggle_@j" href="#" class="btn btn-xs"><i class="glyphicon glyphicon-chevron-up"></i></a>
<script>
$('#menu-toggle_@j').click(function () {
$(this).find('i').toggleClass('glyphicon glyphicon-chevron-up').toggleClass('glyphicon glyphicon-down');
$(if('#menu-toggle_@j').toggleClass('glyphicon glyphicon-down')){
非常感谢任何帮助。
答案 0 :(得分:2)
如果按类确定按钮的状态,则可以轻松检索按钮的状态。只需使用 public static BufferedImage rotate(BufferedImage imgOld, int deg){ //parameter same as method above
BufferedImage imgNew = new BufferedImage(imgOld.getWidth(), imgOld.getHeight(), imgOld.getType()); //create new buffered image
Graphics2D g = (Graphics2D) imgNew.getGraphics(); //create new graphics
g.rotate(QaDMath.toRadians(deg), imgOld.getWidth()/2, imgOld.getHeight()/2); //configure rotation
g.drawImage(imgOld, 0, 0, null); //draw rotated image
return imgNew; //return rotated image
}
:
$.hasClass()
$('button').click(toggleBtn);
$('#getEveryStatus').click(getEveryStatus);
function toggleBtn(){
$(this).toggleClass('on');
if($(this).hasClass('on')){
$(this).next('.status')
.text('The button above is now ON.');
} else {
$(this).next('.status')
.text('The button above is now OFF.');
}
}
function getEveryStatus(){
var list = '';
$('button').each(function(i){
list += 'Button #' + i + ' is ' + ($(this).hasClass('on')? 'ON':'OFF') + '.\n';
});
alert(list);
}
button{
width: 4em;
height: 2em;
border-radius: 2em;
border: none;
outline: none;
position: relative;
background: #ddd;
cursor: pointer;
-webkit-transition: background .2s ease;
transition: background .2s ease;
}
button.on{
background: #63e63e;
}
button:before{
content: '';
position: absolute;
top: 50%;
left: 3em;
height: 2em;
width: 2em;
margin-top: -1em;
margin-left: -1em;
background: #fff;
border-radius: 2em;
box-shadow: 0 0 5px rgba(0,0,0,.5);
-webkit-transition: left .2s ease;
transition: left .2s ease;
}
button.on:before{
left: 1em;
}
#getEveryStatus{
color: blue;
text-decoration: underline;
cursor: pointer;
}