如何添加alpha过滤器到任何HTML元素,并保留其他过滤器在IE? [英] How to add alpha filter to any HTML element and keep the other filters in IE?

查看:128
本文介绍了如何添加alpha过滤器到任何HTML元素,并保留其他过滤器在IE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个HTML

 < img src =aaa.pngid =astyle = filter:alpha(opacity = 100)/> 

然后这个javascript在IE6中工作

  document.getElementById(a)。filters.alpha.opacity = 60; 

但如果未设置样式

 < img src =aaa.pngid =astyle =/> 

javascript引发错误'filters.alpha'为空或不是对象



此代码适用

  document.getElementById ).style.filter =alpha(opacity = 60); 

但是,应用于图像的其他过滤器将被覆盖。所以问题是:如何向任何HTML元素添加Alpha过滤器并在IE中保留其他过滤器?



strong>我想要纯粹的javascript(不是jQuery)解决方案

解决方案

不幸的是, style.filter 属性和过滤器只能操作已有的。



过滤器是一个集合对象,您可以在这里找到文档:过滤器集合。它为您提供了一个非常简单的方式来使用您的现有过滤器,您可以打开和关闭它们(启用)等。 p>

例如,您可以使用

  obj.filters.item DXImageTransform.Microsoft.Alpha)。opacity = 20; 

或(如果alpha是您首次筛选声明)

  obj.filters.item(0).opacity = 20; 

CLASSES



大多数情况下,你最好在CSS中的某些类下存储你的过滤器声明,并且只使用JS来分配正确的类,而不是直接操作 style


If I have this HTML

<img src="aaa.png" id="a" style="filter: alpha(opacity=100)"/>

Then this javascript works in IE6

document.getElementById("a").filters.alpha.opacity = 60;

But if no style is set

<img src="aaa.png" id="a" style=""/>

The javascript throws an error 'filters.alpha' is null or not an object

This code works

document.getElementById("a").style.filter = "alpha(opacity=60)";

But then the other filters applied to the image are overwritten. So the question is: How to add alpha filter to any HTML element and keep the other filters in IE?

edit I would like pure javascript (not jQuery) solution

解决方案

Unfortunately, it seems to me you can only add new elements through the style.filter property, with filters you can only manipulate already existing ones.

filter is a collection object, you can find the docs here: filters Collection. It gives you a nice and easy way to play with your existing filters, you can turn them on and off (enabled), etc.

For example, you can use

obj.filters.item("DXImageTransform.Microsoft.Alpha").opacity=20;

or (if alpha was you first filter declaration)

obj.filters.item(0).opacity=20;

CLASSES

Most of the time you're better off storing your filter declarations under certain classes in your CSS, and only using JS to assign the right classes instead of manipulating style values directly.

这篇关于如何添加alpha过滤器到任何HTML元素,并保留其他过滤器在IE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆