注:如下描述只適用於 IE 的 Standard 模式,未在 Quriks 模式下測試過。javascript
IE6 不支持這兩個屬性,但它會把 width / height 視爲 min-width / min-height
所以,跨瀏覽器的實現方式是這樣的:css
_width: x; min-width: x;
IE6/7 不支持 display: inline-block;。要讓它們實現此效果分兩種狀況:html
display: inline-block;
或 *zoom: 1;
只要對行內元素觸發 hasLayout,便可實現 inline-block,上面兩句都能達到這個效果。通常用前面那句。java
注意:這種狀況下,若當前元素的 margin 的單位是 em,那麼它參照的是父元素設置的字體大小。緣由不明。segmentfault
<style> html, body { padding: 0; margin: 0; } #wrap { font-size: 100px; } a { border: 1px solid #ccc; display: inline-block; font-size: 12px; margin: 1em; padding: 1em; } </style> <div id="wrap"> <a href="">abc</a> <a href="">def</a> </div>
讓塊狀元素實現 inline-block,須要在觸發 hasLayout 以後,將其設爲 display: inline;
兩種作法:瀏覽器
div{ display: inline-block;} div{ *display: inline;}
注意,兩句必須放在兩個聲明中測試
div{ display: inline-block; /*for other*/ *display: inline; *zoom: 1; /*for ie*/ }
這樣的好處是沒必要放在兩個聲明裏了,但多了一條語句。我的推薦這一個。
並且這個方法實際對行內和塊狀元素都有效了,比較便於重用。字體
在現代瀏覽器裏,全局的 resize 事件只在窗口大小變化時被觸發
但在 IE6/7/8 裏,body 元素大小的變動也會觸發這一事件:spa
<div id="content">abc</div> <script> window.onresize = function() { alert('resized'); }; setTimeout(function() { document.getElementById('content').style.height = '300px'; }, 500); </script>
所以,有時窗口大小隻改變一次,卻會觸發兩次 resize 事件
有的狀況下,甚至會致使死循環(即:在響應 resize 事件時,由於修改文檔內容/樣式,再次觸發了 resize 事件)code
解決辦法,用這種方式監聽 resize 事件:
var currheight, currwidth; window.onresize = function() { if(currheight != document.documentElement.clientHeight && currwidth != document.documentElement.clientWidth) { alert('resized'); } currheight = document.documentElement.clientHeight; currwidth = document.documentElement.clientWidth; };
P.S. 思路來自 這裏,原文中的辦法有問題,在這作了一點改進
在 IE6/7 中,若是行內元素的上/下方與 body 之間沒有被其餘東西分隔開,那麼它們的上、下邊框會被"吞掉"
<style>span { border: 1px solid #f00; }</style> <span>abc</span> <span>123</span>
此問題的具體表現以下: