My question is if there is a way to access the content of HTML <style>
element from jQuery.
<style>
.calendarNavbarLink {
margin-left: 20px;
}
.today {
background-color: lawngreen;
}
.weekend {
background-color: lightsalmon;
}
</style>
For example: I would access the value of the property background-color
of the rule .weekend
. Is it possible do this from jQuery?
2
Answers
Use
styleSheets
property of JS to get the styleSheets content.After this, you can loop through the classes, to get the desired result.
Refer:- https://developer.mozilla.org/en-US/docs/Web/API/Document/styleSheets
Step 1:
By using a couple of CSSOM (CSS Object Model) properties, you can query your stylesheet and build an easy-to-search array of selectors and style rules:
Having done this, if you now run:
you will get the following
array
:Step 2:
You now have an array you can parse with a helper function.
E.g. If you want to know
you can run:
which returns the following result: