为不同的浏览器载入不同CSS的二种方法
2008-10-26 20:46
288 查看
由于各浏览器(IE、gecko、opera......)对CSS的渲染方式都有所不同,造成显示效果不同,所以可通过对其载入不同CSS的方法来解决,下面介绍下二种方法来实现,不一定很实用,难的玩一把。
纯CSS方式
.box{ background: #808080;
}
以上代码运行的结果,在IE中是灰色,Opera中是紫红,Firefox中是蓝色
在IE7以前版本还可采用CSS2的属性选择符来区别,不幸的是IE7开始已能识别。
本文章由forestgan于15-Oct-2006发表
javascript的方法
此脚本能区别更多的浏览器,具体使用方法可看脚本原作者网站。
function Preview(obj)
{
var TestWin=open('');
TestWin.document.write(obj.value);
}
function copyCode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.writeln(obj.value);
winname.document.execCommand('saveas','','homepage.yesky.com.htm');
winname.close();
}
function addBookmark(title,url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( document.all ) {
window.external.AddFavorite(url,title);
} else if( window.opera && window.print ) {
return true;
}
}
<!--[if IE]><style>
@import url("/tutorials/demo/css/ie.css");
</style><![endif]-->
<style type="text/css">
<!--
p{
padding: 1em;
color: #FFF;
text-align: center;
}
h3{
fon-size: 1.5em;
}
.box{
width: 20em;
height:20em;
background: #369;
margin: 2em auto;
}
@media all and (min-width: 0px){
box{
background: #C00;
}
}
-->
</style>
<!--[if IE ]><style>
.box{
background: #808080;
}
</style><![endif]-->
CSS Browser Selector
<div class="box">
<p>IE中是灰色</p>
<p>Opera中是紫红</p>
<p>Firefox中是蓝色</p>
</div>
如果不能正常显示,请刷新页面。
运行代码复制代码另存代码收藏本页
纯CSS方式
.box{ background: #808080;
}
以下是引用片段: Code .box{ width: 20em; height:20em; background: #369; } /*--gecko内核--*/ @media all and (min-width: 0px){ box{ background: #CC0000; } } /*--opera hacks--*/ /*--不能通过W3C CSS检验--*/ <!--[if IE ]><style> .box{ background: #808080; } </style><![endif]--> |
在IE7以前版本还可采用CSS2的属性选择符来区别,不幸的是IE7开始已能识别。
本文章由forestgan于15-Oct-2006发表
javascript的方法
以下是引用片段: var css_browser_selector = function() { var ua = navigator.userAgent.toLowerCase(), is = function(t){ return ua.indexOf(t) != -1; }, h = document.getElementsByTagName('html')[0], b = (!(/opera|webtv/i.test(ua)) && /msie (/d)/.test(ua)) ? ((is('mac') ? 'ieMac ' : '') + 'ie ie' + RegExp.$1) : is('gecko/') ? 'gecko' : is('opera') ? 'opera' : is('konqueror') ? 'konqueror' : is('applewebkit/') ? 'webkit safari' : is('mozilla/') ? 'gecko' : '', os = (is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac' : is('win') ? ' win' : ''; var c = b+os+' js'; h.className += h.className?' '+c:c; }(); |
function Preview(obj)
{
var TestWin=open('');
TestWin.document.write(obj.value);
}
function copyCode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.writeln(obj.value);
winname.document.execCommand('saveas','','homepage.yesky.com.htm');
winname.close();
}
function addBookmark(title,url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( document.all ) {
window.external.AddFavorite(url,title);
} else if( window.opera && window.print ) {
return true;
}
}
<!--[if IE]><style>
@import url("/tutorials/demo/css/ie.css");
</style><![endif]-->
<style type="text/css">
<!--
p{
padding: 1em;
color: #FFF;
text-align: center;
}
h3{
fon-size: 1.5em;
}
.box{
width: 20em;
height:20em;
background: #369;
margin: 2em auto;
}
@media all and (min-width: 0px){
box{
background: #C00;
}
}
-->
</style>
<!--[if IE ]><style>
.box{
background: #808080;
}
</style><![endif]-->
CSS Browser Selector
<div class="box">
<p>IE中是灰色</p>
<p>Opera中是紫红</p>
<p>Firefox中是蓝色</p>
</div>
如果不能正常显示,请刷新页面。
运行代码复制代码另存代码收藏本页
相关文章推荐
- 为不同的浏览器载入不同CSS的二种方法CSS Browser Selector
- CSS针对不同的浏览器的适应方法
- 记录下遮罩的方法 css不同浏览器下的 透明 设计
- js调用css的问题,适应不同浏览器对ID的调用方法
- 让css在不同浏览器下正常运行的方法——使用条件注释
- css在各浏览器不同处理方法
- Responsive design 根据浏览器大小应用不同css的方法
- 针对不同浏览器获取到css文件里相关属性的两种方法
- IE, FireFox, Opera 浏览器支持CSS实现Alpha透明的方法 兼容问题
- selenium webdriver启动不同浏览器驱动的方法
- CSS - 针对不同浏览器的写法
- css中使用if条件在各大浏览器(IE6\IE7\IE8)中hack方法解决教程
- 为 SELECT.options 对象增加或删除选项的方法在各浏览器中的支持情况不同
- CSS对IE7,IE6,FireFox和其它不同浏览器的控制——CSS设计中的HACK,CSS解决方案
- css 在ie firefox下宽度不同的解决方法
- IE, FireFox, Opera 浏览器支持CSS实现Alpha透明的方法 兼容问题
- 字符串拼接方法在不同浏览器引擎的差异测试结论
- 浏览器版本不同导致文件无法下载问题解决方法
- 用不同的方法应用CSS(转)
- 解决不同浏览器显示空格不一样的方法