Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

javascript - How to add alpha filter to any HTML element and keep the other filters in IE?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...