Peculiar Use of Conditional Comments in IE
I ran into a use of IE conditional comments today that I had never seen before and which has me scratching my head. The HTML starts like this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--[if IE 7 ]> <html class="no-js ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]> <html class="no-js ie8" lang="en"> <![endif]-->
...
</html>
I wondered how IE7 would deal with this document. It appears that the document would end up with two html tags, the first being unclosed. Surely that wouldn't parse. But it does. I decided to investigate.
In the IE7 browser mode of IE9, the html tag ends up as
<html class="no-js ie7" lang="en" xmlns="http://www.w3.org/1999/xhtml">
It appears that the attributes from the included conditional comment's html tag are being merged with the existing html tag. Upon further tinkering, it appears that attributes in the conditional html tag are added to the existing tag if they don't already exist in the first html tag. So for example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html class="not so fast" xmlns="http://www.w3.org/1999/xhtml">
<!--[if IE 7 ]> <html class="no-js ie7" lang="en"> <![endif]-->
results in:
<html class="not so fast" lang="en" xmlns="http://www.w3.org/1999/xhtml" sizcache="0" sizset="0">
Here, the conditionally included html tag's class attribute has had no effect because the unconditional html tag already defines a class attribute. Where the sizcache and siz开发者_如何学Goset attributes come from is a complete mystery.
Anyway, none of this observed behaviour is what I would have expected. The Microsoft documentation on conditional comments doesn't mention anything about this use of conditonal comments, and googling has come up dry. The page later includes a style sheet that has selectors that reference the ie7 and ie8 classes to override the standard styles in situations where IE 7 and 8 render them incorrectly.
I'm just curious if anyone has seen this use of conditional comments or knows if this blending behavior is documented anywhere.
This looks like a variation of a technique developed by Paul Irish but one that is incomplete and I suspect less predictable.
精彩评论