Hiding a div with two styles classes added to it
I have a div with two classes added to it , now i want to completely hide this div(and this div doesn't have any id).
for e.g.
<div class="class1 class2">
Hello!!
</div>
Can anyone please suggest how this开发者_C百科 can be achieved?
Thanks
That's pretty easy: $('div.class1.class2').hide();
Note that it will select all matching divs. If you only want to select a single one you need to use a more specific selector or an ID.
Try this:
$("div.class1").filter("div.class2").hide();
This selects all div
elements with 'class1' and then filters out the submet that has 'class2' as well. It then calls the hide()
method to hide it.
Or more simply (thanks @thiefmaster)
$("div.class1.class2").hide();
use a class selector
$('.class1').hide();
You can use this way to hide the complete div.
$(".class1").hide();
$('div.yourClass').css('display', 'none');
or
$('div.yourClass').hide();
which pretty much do the same thing.
or
$('div.yourClass').css('visibility', 'hidden');
or
$('div.yourClass').toggle();
which also perform pretty much the same thing.
Or you could define a new css class .hideMyDiv
CSS:
div.hideMyDiv {display:none;}
jQuery:
$('div.hideMyDiv').addClass('hideMyDiv');
Several of the other answers are basically correct, however, the one thing I would add is 'it depends'... it depends on if you have other elements with these same classes. If this is the only element with either of these classes, you can use either $('.class1').hide();
or $('.class2').hide();
. If this is the only one with both, then $("div.class1").filter("div.class2").hide();
is better. (Note that the addition of div
will exclude other elements with these classes from the selection.)
If there are other divs with both classes and you can't add id's, another option is to look at the position of the div you want to hide. For example, if you know you only want to hide the first one, you could use $("div.class1").filter("div.class2").filter(":first").hide();
精彩评论