Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add css style to the all children of element at the same time?

Tags:

jquery

css

Such as:

<div class="mostOut">
    <div class="a">
        <h3 class="b"></h3>
        <div class="d"></div>
    </div>
</div>

And how can I apply css style to the children of "moustOut" at the same time.E.g:

with one click event.the css('opacity',1) could apply to the "a","b","d" at the same time.Or ,exclude "b" .

like image 771
qinHaiXiang Avatar asked Nov 30 '10 16:11

qinHaiXiang


People also ask

How do I apply the same style to multiple elements in CSS?

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.

How do I apply CSS to all child except last?

How to select all children of an element except the last child using CSS? When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes.

How do I apply CSS to all elements except one?

Approach: Use the :not(selector), also known as negation pseudo-class which takes a simple selector as an argument and allows you to style all the elements except the element specified by the selector.


2 Answers

a little less code

$('.mostOut').click(function()
{
   $(this).children("*").css('opacity', 1);
});

and exclude ".b"

$('.mostOut').click(function()
{
   $(this).children("*").not(".b").css('opacity', 1);
});

or alternatively if its not the opacity causing the hidden/none

$('.mostOut').click(function()
{
   $(this).children("*").show();
});
like image 178
Hans Avatar answered Oct 25 '22 15:10

Hans


This might be a dirty solution, but it should work:

$('.mostOut').click(function()
{
  $(this).find('*').each(function()
  {
    $(this).css('opacity', 1);
  });
});

For excluding all but the b element, try this:

$('.mostOut').click(function()
{
  $(this).find('*').not('.b').each(function()
  {
    $(this).css('opacity', 1);
  });
});

Play with the code, as I just typed this off of the top of my head, so I'm not guaranteeing that it will work.

Good luck!

like image 31
Blender Avatar answered Oct 25 '22 15:10

Blender