The question is simple; using jQuery's css
function, the computed style of a CSS attribute may be returned, but what if there are more than one style for that attribute being rendered? For example :
<div id="foo" style="text-decoration:underline;">Some underline text</div>
The instruction $('#foo').css('text-decoration');
will return underline
. Now if I change it to
<div id="foo" style="text-decoration:underline;">Some underline <span id="bar" style="text-decoration:line-through;">text</span></div>
The instruction $('#bar').css('text-decoration');
will return line-through
, alright.
But the actual text is also underline
! How can I return both? Do I need to search all ancestors if I want to know if some text is both underline
and line-through
? Sounds a bit painful, no?
** Edit **
Another problem arises whith this HTML
<span style="text-decoration:underline;">some <span id="e1" style="font-weight:bold;">text</span></span>
where $('#e1').css('text-decoration');
returns none
for some reason, while the text is clearly rendered with an underline.
** Disclaimer **
This question is not to debate how the UA renders an element, but if an element hierarchy applies a CSS or not. If one wants to understand text-decoration
better, I suggest one would read about it. The question tries to focus on a more generalize matter. For example, it can also apply to this HTML
<div style="display:none;">Some <span id="keyword" style="text-decoration:underline;">hidden</span> text</div>
where one could want to know if the element keyword
is visible or not. With the code below, this is simply done with
cssLookup($('#keyword'), 'display', 'none'); // -> true
** UPDATE **
After all the answers and comments, here is, based on Brock Adams solution :
/**
* Lookup the given node and node's parents for the given style value. Returns boolean
*
* @param e element (jQuery object)
* @param style the style name
* @param value the value to look for
* @return boolean
*/
function cssLookup(e, style, value) {
var result = (e.css(style) == value);
if (!result) {
e.parents().each(function() {
if ($(this).css(style) == value) {
result = true;
return false;
}
});
}
return result;
}
Thank you, everyone, for your inputs.
Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2....). You can apply as many properties as you like in a single call. Here you can pass key as property and val as its value as described above.
Projects In JavaScript & JQuery To define multiple CSS attributes in jQuery, use the css selector or the addClass() method.
I don't think any browser, or the W3C, provides a good way to do this.
A complicating factor is knowing which styles cancel preceding styles (underline versus no-underline, for example).
So, we would need multiple look-up tables or human judgement to know which style actually applied.
Finally, all these methods (3 answers so far) cannot distinguish between a blank, or missing, style setting and an explicitly set none
. Obviously the browser can render an explicitly set none
differently than a blank or missing setting.
For human use, this code should do the trick:
function cssTree (jNode, styleName, bShowBlanks) {
var styleArray = [jNode.css (styleName)];
jNode.parents ().map ( function () {
var style = $(this).css (styleName);
if (bShowBlanks || ! /^(none|\s*)$/i.test (style) )
styleArray.push (style);
} );
return styleArray;
}
alert ( cssTree ( $("#bar"), 'text-decoration') );
See it in action at jsFiddle.
Results:
bar: line-through,underline
el: none,underline
//-- With bShowBlanks = true.
bar: line-through,underline,none,none
el: none,underline,none,none
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