加龙

加龙加香不加价
随笔 - 133, 评论 - 837 , 引用 - 51

IE和Firefox下CSS和Javascript的区别

1、浏览器事件的捕捉

在IE下有一个全局的window.event,当事件触发后可以直接使用,但是在fireFox下没有这个东西,当调用触发事件调用一个函数时,如果这个函数没有形参,那么firefox会默认的把event(事件)传进去,但是有参数时就不行啦,所以解决的办法是,自己手动传一个event进去,这样就ok了,具体代码如下:

下面两个函数,都是响应鼠标onclick时触发的动作,第一个在ie下使用正常,但是在firefox下却有问题,改成第二个那样使用,就没有问题了,注意调用方法的区别

view plaincopy to clipboardprint?
<html>
<head>
<title>test</title>
<script language="javascript">
function testevent()
{
window.alert(window.event.target.id);
return;
}
</script>
</head>
</body>
<a href="#" onclick="testevent()" id="alink">testevent</a>
</body>
</html>
<html>
<head>
<title>test</title>
<script language="javascript">
function testevent()
{
window.alert(window.event.target.id);
return;
}
</script>
</head>
</body>
<a href="#" onclick="testevent()" id="alink">testevent</a>
</body>
</html>


view plaincopy to clipboardprint?
<html>
<head>
<title>test1</title>
<script language="javascript">
function testevent(evt)
{
window.alert(evt.target.id);
return;
}
</script>
</head>
</body>
<a href="#" onclick="testevent(event)" id="alink">testevent</a>
</body>
</html>
<html>
<head>
<title>test1</title>
<script language="javascript">
function testevent(evt)
{
window.alert(evt.target.id);
return;
}
</script>
</head>
</body>
<a href="#" onclick="testevent(event)" id="alink">testevent</a>
</body>
</html>


其实event对象在ie以及firefox还有很多不同的特性,比如clienx,pagex等,但是由于在现在流行使用的js框架prototype中解决了很多这些问题,所以如果是在基于prototype下的开发,这些问题可以考虑得少一些了,只是上面提到的这个捕获问题,prototype中并没有完善的解决,所以单独列出来,下面提及的关于js的也都只列出prototype中未解决的

2、关于透明度的设置

为了达到给层设置半透明的效果时,在IE和firefox下也有所不同,IE下,style的filter属性有Alpha值可供使用,而firefox下没有Alpha值,所以得指定style的MozOpacity,代码见下:

<STYLE>
filter: Alpha(opacity=10); /*IE*/
-moz-opacity:.1; /*老版本FireFox 1.0 以前*/
opacity:0.1; /*新版本FireFox*/
</STYLE>

view plaincopy to clipboardprint?
<script language="javascript">
//设置一个id为screen的div的透明度为45%,在IE下:
document.getElementById(screen).style.filter=Alpha(Opacity=45);

//而在firefox下:
document.getElementById(screen).style.MozOpacity=0.45;
</script>
<script language="javascript">
//设置一个id为screen的div的透明度为45%,在IE下:
document.getElementById(screen).style.filter=Alpha(Opacity=45);

//而在firefox下:
document.getElementById(screen).style.MozOpacity=0.45;
</script>


3、定位层时的有趣问题

在定位层时,我们的做法是给层的style.left 和 style.top设置位置,但是今天发现了一个很有趣的问题,代码如下:

view plaincopy to clipboardprint?
<script language="javascript">
//给一个id为dialog的层定位
document.getElementById(dialog).left = 100;
document.getElementById(dialog).left = 100;

//问题出现了,在ie下,默认将层的左上角定位在(100px,100px)这个点上
//但是在firefox下却死活都不行,后来发现,原来ie在你没有指定单位的时候
//替你加上了单位“px”,而firefox比较“笨”
//他觉得你没有指定单位,就不给你定位,好了,那么标准的写法应该是这样:

document.getElementById(dialog).left = 100px;
document.getElementById(dialog).left = 100px;

//这样firefox也认了

</script>
<script language="javascript">
//给一个id为dialog的层定位
document.getElementById(dialog).left = 100;
document.getElementById(dialog).left = 100;

//问题出现了,在ie下,默认将层的左上角定位在(100px,100px)这个点上
//但是在firefox下却死活都不行,后来发现,原来ie在你没有指定单位的时候
//替你加上了单位“px”,而firefox比较“笨”
//他觉得你没有指定单位,就不给你定位,好了,那么标准的写法应该是这样:

document.getElementById(dialog).left = 100px;
document.getElementById(dialog).left = 100px;

//这样firefox也认了

</script>


4、PNG透明背景的问题
PNG图片在网站设计中是不可或缺的部分,最大的特点应该在于PNG可以无损压缩,而且还可以设置透明,对于增强网站的图片色彩效果有重要的作用。

但为什么PNG图片却没有GIF和JPG图片的使用来得广泛呢,这个祸因应归属于微软的IE浏览器(Firefox和Opera对PNG支持的比较好,而现在浏览器的主流IE6却无法很好的支持)。不过微软在最近也开始改过自新了,新出的的IE7可以很好的支持PNG,可以想象在未来的网络世界,PNG图片的重要性将会更加凸显。

但在大家还在绝大多数的使用IE6的时候,我们又怎样在IE6的世界去完美使用PNG图片呢(PNG图片的时候最重要的地方在于PNG透明背景图片的运用)。我们应该庆幸我们是幸福的!IE5.5+的AlphaImageLoader滤镜为通向png提供了一个道路,如果他载入的是PNG(Portable Network Graphics)格式,则0%-100%的透明度也被提供。但IE5.0无法支持属性,那只有完全绝望了,不过绝望的只是几个,得到是绝大数,我们应该知足,知足才会常乐。

现在我们将通过Hack和AlphaImageLoader滤镜来实现IE6下的PNG透明背景图片。

先熟悉下滤镜的语法:

view plaincopy to clipboardprint?
filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )

属性:

enabled : 可选项。布尔值(Boolean)。设置或检索滤镜是否激活。true | false
true :  默认值。滤镜激活。
false :  滤镜被禁止。

sizingMethod : 可选项。字符串(String)。设置或检索滤镜作用的对象的图片在对象容器边界内的显示方式。
crop : 剪切图片以适应对象尺寸。
image : 默认值。增大或减小对象的尺寸边界以适应图片的尺寸。
scale : 缩放图片以适应对象的尺寸边界。

src :  必选项。字符串(String)。使用绝对或相对 url 地址指定背景图像。假如忽略此参数,滤镜将不会作用。
filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )

属性:

enabled : 可选项。布尔值(Boolean)。设置或检索滤镜是否激活。true | false
true :  默认值。滤镜激活。
false :  滤镜被禁止。

sizingMethod : 可选项。字符串(String)。设置或检索滤镜作用的对象的图片在对象容器边界内的显示方式。
crop : 剪切图片以适应对象尺寸。
image : 默认值。增大或减小对象的尺寸边界以适应图片的尺寸。
scale : 缩放图片以适应对象的尺寸边界。

src :  必选项。字符串(String)。使用绝对或相对 url 地址指定背景图像。假如忽略此参数,滤镜将不会作用。


Firefox、Opera等完全支持PNG透明图片的浏览器也支持子选择器(>),而IE不识别(包括IE7),所有我们可以通过这来定义Firefox、Opera等浏览器中PNG图片的样式。如下

view plaincopy to clipboardprint?
<script language="javascript>
//给一个id为infoBox的层设置一个透明背景,背景图片是down.png,代码如下
//进行了浏览器判断

if (navigator.appName!="Microsoft Internet Explorer")
{
$(infoBox).style.background="0 url(down.png) no-repeat";
}
else
{
$(infoBox).style.background="0 none no-repeat";
$(infoBox).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=scale,src=down.png)";
}

</script>
<script language="javascript>
//给一个id为infoBox的层设置一个透明背景,背景图片是down.png,代码如下
//进行了浏览器判断

if (navigator.appName!="Microsoft Internet Explorer")
{
$(infoBox).style.background="0 url(down.png) no-repeat";
}
else
{
$(infoBox).style.background="0 none no-repeat";
$(infoBox).style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=scale,src=down.png)";
}

</script>


不过需要注意的一个地方:

使用AlphaImageLoader 后该区域的超链接和按钮会失效,解决的方法:

对链接或按钮直接设置相对位置,让它们浮动于滤镜区域的上面。

发表于 2008年9月24日 16:55

评论

# re: IE和Firefox下CSS和Javascript的区别

不错,哈哈
2009/7/12 10:27 | 凡客诚品

# re: IE和Firefox下CSS和Javascript的区别

对链接或按钮直接设置相对位置,让它们浮动于滤镜区域的上面
2009/8/23 11:09 | 春水堂

# re: IE和Firefox下CSS和Javascript的区别

Love the post and it took me a long time to scroll down to the bottom with what everyone had to say.
2011/1/10 20:46 | air max 2011

# re: IE和Firefox下CSS和Javascript的区别

Love the post and it took me a long time to scroll down to the bottom with what everyone had to say.
2011/1/10 20:56 | nike size 13

# re: IE和Firefox下CSS和Javascript的区别

Many more people, whoever men or women, youngs or olds, thin or muscular, are wearing ugg boots nowadays. And undoubtedly, there will be more in the days to come. You may wonder how can a pair of boots be so hot and eye-catching, well, people definitely have different attitudes towards this. But people like ugg boot mainly because ugg can keep our feet and body warm, and always be comfortable. Perhaps somebody think uggs are expensive, don't worry it again, now, we proivde you cheap ugg on our website http://www.cheaptalluggs.com You can find many kinds of ugg boots here, and buy ugg boots with low price but high quality. Many kinds of ugg cardy also be on sale, perhaps, you can not help falling in love with it. Now, mens uggs are on hot sale, boys and men can hurry up to own mens ugg boots for the coming cold weather. And, wearing one pair of ugg boot, you'll feel there's one step closer for you to be a star. That's the magical power of ugg.
2011/9/18 10:07 | cheaptalluggs

# coach outlet online

The most incredible knack about the coach bag of coach outlet online is that it would excellently please your minds beyond your mind's eye. For example, if you want to become the Angelina Julie, you will immediately need to wear the coach handbag around your shoulder.On establishing contact with a coach outlet hire agency, a few clarifications would need to be put to rest prior to working out a deal.
2012/1/6 11:42 | coach outlet online

# louis vuitton sale

There must be one louis vuitton vernis that is sure to fit your style. louis vuitton sale provide Louis Vuitton belt for men has royal purple lining, which could burnish whatever clothes.As we show below, louis vuitton outlet have a number of Louis Vuitton Earrings, louis vuitton replica handbags, in different styles for your selection.
http://www.louisvuittonoutletsaletime.com
2012/1/6 13:34 | louis vuitton sale

# coach factory outlet

Welcome to the coach factory outlet store and Enjoy Shopping Here! We promise all the customers to have the superior qualities and low prices.Many people like to go to coach factory online, Some people like to designers and shiny metal or leather coach shoulder bags. However, the majority of women choose to safe the neuter color coach shoulder bags. http://www.coachfactoryoutletlove.com
2012/1/6 13:34 | coach factory outlet

# louis vuitton uk

As we know,
louis vuitton uk

as we can imagine are a little expensive but are of good quality like the brand Coach. It's really a good choice to shopping in Louis Vuitton outlet.
louis vuitton

Outlets offer famous classic brand for LV,Channel, with perfect service.So become to the VIP soon.They offer more new styles,like LV purses,LV wallets etc. And are tested by product quality monitoring center .
http://www.louisvuitton-ukoutlet.org.uk
2012/1/6 13:37 | louis vuitton uk

# louis vuitton outlet

In the web times, even if you are a noble person on
louis vuitton outlet

, or to real action to prove himself, has been integrated into a new era.If in a foreign country to join the twitter, it is best to join army of micro-Bo;They're not chosen, so one of these ideal for you.For more flexibleness a lot more like these, there are lots of discount 'shoulder' variations outlet
louis vuitton bags

.
http://www.louisvuittonoutletbagsu.com
2012/1/6 13:45 | louis vuitton outlet

# coach outlet online

At the
coach outlet online

you have the largest selection of the day. If you touch the item and like it, keep it in your possession until you make your final decision.In particular, products from
coach factory outlet

with leather design are fashionable, handmade,leading the wave of American pop.It with simple,durable and unique style to win consumers.
http://www.coachoutletonlineusacoach.com
2012/1/6 13:46 | coach outlet online

# coach outlet store

Coach Outlet Store vision system is a visual enjoy, is a visual art. The particular design has been integrated function and the element of elegancy, then show you an exquisite and excellent product. You need not worry about the quality of the coach outlet store online for sale now. The Coach brand is famous for the perfect products.

2012/1/6 14:37 | coach outlet store

# coach outlet

if your dream is to look like a million bucks for a mere several dollars, then our coach outlet store is the Jiminy Cricket of your existence. to obtain coach outlet online in Hainan is amongst the brand new darling from residential holiday makers.


2012/1/6 14:37 | coach outlet

# coach factory outlet

Coach Factory Outlet has a big heart and a healthy appetite, and a wicked swing with a chainsaw.
2012/1/7 17:01 | coach factory outlet

Post Comment

主题  
姓名  
主页
校验码  
内容   
京ICP备 05050892号