Using jQuery, how would you find elements which have a particular style (eg: float: left
), regardless of whether it's an inline style or one defined in a CSS file?
The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Id Selectors The id selector is used to define style rules for a single or unique element. The id selector is defined with a hash sign ( # ) immediately followed by the id value.
URLs with an # followed by an anchor name link to a certain element within a document. The element being linked to is the target element. The :target selector can be used to style the current active target element.
You will begin by using the type selector to select HTML elements to style. Then, you will combine selectors to identify and apply styles more precisely. Lastly, you will group several selectors to apply the same styles to different elements.
Using the filter function:
$('*').filter(function() { return $(this).css('float') == 'left'; });
Replace '*' with the appropriate selectors for your case.
This is gonna be slow. Like REALLY slow. If you know you need to select all elements with a given CSS style, you will see much better performance by applying a single additional css rule to each element, and then selecting by that rule.
It's going to be MUCH faster and more readable to boot.
CSS:
.float-left {float:left}
Javascript:
$('.float-left');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With