开发者

Adding an image path to a filter in css by javascript

开发者 https://www.devze.com 2023-04-01 15:17 出处:网络
I have the following for my site\'s background to be scaled when in Internet Explorer: #background{filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'images/background1.jpg\', sizingMe

I have the following for my site's background to be scaled when in Internet Explorer:

#background{filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/background1.jpg', sizingMethod='scale');
-ms-fi开发者_如何学编程lter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/background1.jpg', sizingMethod='scale')";}

That works great, but I need to change the image path to a variable that comes from javascript, from an array with several backgrounds. With a radom number I'm chosing the background everytime the site loads.

To do that I'm doing:

#background{filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='<script>document.write(background[rn]);</script>', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='<script>document.write(background[rn]);</script>', sizingMethod='scale')";}

But this is not working. The scaling does not work anymore.

Why? What's wrong with it? How would you make it work?

Thanks a lot


Does this work?:

<script>
document.write("<style type=\"text/css\">\n#background{filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + background[rn] + "', sizingMethod='scale' \n-ms-filter: \"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + background[rn] + "', sizingMethod='scale')\";}\n</style>");
</script>

In your example you were trying to insert a script tag inside quotes, this is not possible as it would just be interpreted as a string and not a script.


For anyone else who runs into this problem you cannot dynamically change this attribute when it is being applied to the body tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号