Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to use $(this) and universal selector (*)?

I want to know if it is possible to use universal selector with $(this). When, for example, I want delete all inline CSS from the element and its children I use this code:

$('#element, #element *').attr('style','');

But if i have $(this) what is the code?

Maybe I have to try:

 $(this, this+'*').attr('style','');
like image 917
Borja Avatar asked Dec 27 '15 16:12

Borja


People also ask

What does the universal selector (*) Select?

Universal Selector in CSS is used to select all the elements on the HTML page. It is denoted by an asterisk (*).

What is the function of the universal selector asterisk (*) in this example?

The Universal Selector is the * in CSS. Literally the asterisk character. It is essentially a type selector that matches any type. Type meaning an HTML tag like <div> , <body> , <button> , or literally any of the others.

Can universal selectors can be used in combination with other selectors?

Universal CSS Selector The three lines of code inside the curly braces ( color , font-size , and line-height ) will apply to all elements on the HTML page. As seen here, the universal selector is declared using an asterisk. You can also use the universal selector in combination with other selectors.

How do you use a universal selector?

Universal Selector It matches a single element. An asterisk ( i.e. "*" ) is used to denote a CSS universal selector. An asterisk can also be followed by a selector. This is useful when you want to set a style for of all the elements of an HTML page or for all of the elements within an element of an HTML page.


1 Answers

You can either use find()

$(this).find('*').attr('style','');

or the context selector

$('*', this).attr('style','');

to do the same thing as $('#element *') with this

If the element represented by this should be part of the collection, you can add it back

$(this).find('*').addBack().attr('style','');

I tend to agree with Rory McCrossan in the comments, using a parent element and external styles would be preferable to changing the style attribute on multiple elements

.element.styled * { /* styles for all children */ }

then just

$('.element').removeClass('styled')

to remove the styles on the children

like image 169
adeneo Avatar answered Sep 25 '22 16:09

adeneo