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.
text-decoration
now? – Yanick Rochon Jul 14 '11 at 4:36