admin管理员组文章数量:1313599
i have a show/hide div javascript function
i want convert it to work with class name instead of style
This is my function that i want convert it
<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>
i want convert it to work with class name,
for example:
#hidden{
display:none;
}
#shown{
display:block;
}
function toggle(e,id){
if(el.class=='hidden'){
el.setAttribute("class", "showen");
}
//etc...
}
i have a show/hide div javascript function
i want convert it to work with class name instead of style
This is my function that i want convert it
<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>
i want convert it to work with class name,
for example:
#hidden{
display:none;
}
#shown{
display:block;
}
function toggle(e,id){
if(el.class=='hidden'){
el.setAttribute("class", "showen");
}
//etc...
}
Share
Improve this question
asked Jun 27, 2012 at 15:13
Alaa GamalAlaa Gamal
1,1357 gold badges18 silver badges28 bronze badges
3 Answers
Reset to default 3A little off-topic, but this is even easier if you're using the jquery library.
$(".yourclass").hide();
$(".yourclass").show();
To test if something is showing with jquery:
if ($(".yourclass").is(":visible"))
Obviously there's reasons you might not want to be using jquery, but it has some nifty tools to make things like this easier.
First and foremost your CSS is invalid, classes are defined in CSS using . and not #. #'s define IDs, which are always unique to one element in the document. A class on the other hand, is as the name implies, a type of thing that many objects can be. An ID is like the name of a dog "Fido", you should have only one dog named "Fido", whereas a class is like a dog breed, you could have many cockapoos, but each must have a unique name. The other answers to this question fail to correct your CSS, and if you don't correct that, it won't work.
.hidden{
display:none;
}
.shown{
display:block;
}
Secondly, it would make your life a lot easier if you used a JavaScript framework, such as JQuery. Here is the same thing written using JQuery (Jquery library include omitted)
$(document).click(function() {
var toggleElements = $(".toggleMe");
$.each(toggleElements, function (key, value) {
if (value.hasClass('hidden')) {
value.removeClass('hidden');
value.addClass('shown');
} else {
if (value.hasClass('shown')) {
value.removeClass('shown');
value.addClass('hidden');
}
}
});
});
The other thing I'd remend to improve this would be to instead of using display: block to show, because display:block doesn't necessarily imply simply "shown" vs. "hidden", it implies information about how that element is handled in terms of display, meaning a block element is treated as its full height/width with whitespace as needed no matter, what and any HTML elements next to it are forced to the next line unless explicitly designed otherwise. What you should do instead is simply remove the hidden class.
Checking for the existing of a class name in className
is not as straightforward as one might think. The following article has an excellent explanation of this and includes the custom function: hasClassName()
function hasClass(el, class_to_match) {
if (el && el.className && typeof class_to_match === "string") {
var c = el.getAttribute("class");
c = " " + c + " ";
return c.indexOf(" " + class_to_match + " ") > -1;
}
else {
return false;
}
}
Then you can simply update your toggle
function:
function toggle(el, id) {
if (hasClassName(el, 'hidden')) {
el.setAttribute("class", "showen");
}
}
Note: I do advocate using such a Javascript framework/library if you are doing such things often. But if toggling an element's display is only things you do, then I believe they're overkill.
本文标签: javascriptshowhide div based on class nameStack Overflow
版权声明:本文标题:javascript - showhide div based on class name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741928329a2405427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论