Make a div tag toggle between two css classes
This question already has an answer here:
You can use
if(document.getElementById("MyId").className == "hide")
document.getElementById("MyId").className = "show";
else
document.getElementById("MyId").className = "hide";
But you need first to give an ID to the div.. Please find below a working example: http://jsfiddle.net/HvmmY/
If you are just trying to show / hide an element, you can use the style.display property :
hidebutton.onclick = function(){
var div = document.getElementsByClassName('show').item(0);
if(div.style.display == '') {
div.style.display = 'none';
} else {
div.style.display = 'block';
}
}
Hope it helps
I like to use jquery for adding/removing <div>
classes. However, it might not work in your situation.
You might be able to use jQuery on it with toggle
:
$('#box').click(function() {
$('#box2').toggleClass('show hide');
});
Working example: http://jsfiddle.net/mFA5F/1/
Information on toggle()
can be seen here: http://api.jquery.com/toggle/
上一篇: Javascript类名称操作不起作用
下一篇: 让一个div标签在两个css类之间切换