我有一个显示/隐藏 div javascript 函数
我想将其转换为使用类名而不是样式
这是我想要转换的函数
<script>
function toggle(e, id) {
var el = document.getElementById(id);
el.style.display = (el.style.display == 'none' || el.style.display == '') ? 'block' : 'none';
toggle.el = el;
if (e.stopPropagation) e.stopPropagation();
e.cancelBubble = true;
return false;
}
document.onclick = function() {
if (toggle.el) {
toggle.el.style.display = 'none';
}
}
</script>
我想将其转换为与类名一起使用,
例如:
#hidden{
display:none;
}
#shown{
display:block;
}
function toggle(e,id){
if(el.class=='hidden'){
el.setAttribute("class", "showen");
}
//etc...
}
最佳答案
有点离题,但如果您使用 jquery 库,这会更容易。
$(".yourclass").hide();
$(".yourclass").show();
要测试是否使用 jquery 显示某些内容:
if ($(".yourclass").is(":visible"))
显然,您可能不想使用 jquery,但它有一些漂亮的工具可以使事情变得更容易。
关于javascript - 根据类名显示/隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11229474/