简体   繁体   English

如何在没有CSS的情况下更改PNG颜色?

[英]How to change a PNG color without CSS?

So I have an <img> tag with a .png source and I need to apply a color change to it. 因此,我有一个带有.png来源的<img>标记,我需要对其应用颜色更改。 I tried the filter property in CSS but apparently it's not supported in IE. 我在CSS中尝试了filter属性,但显然IE不支持该属性。 Any idea if I can do that with JavaScript? 我可以使用JavaScript知道吗?

Maybe you can achieve this in two way: 也许您可以通过两种方式实现这一目标:

  1. by using different png source files, changing them at the occurrence of an event: 通过使用不同的png源文件,在事件发生时更改它们:

    • with pure JavaScript: 使用纯JavaScript:

      var image = document.getElementById("my_image");

      image.src = "image_1.jpg";

    • with jQuery: 使用jQuery:

      $("#my_image").attr("src","image1.jpg");

  2. by giving a transparency to your png to show a background-color applied below it: 通过为您的png赋予透明度,以显示在其下方应用的background-color

    • with pure JavaScript: 使用纯JavaScript:

      var image = document.getElementById("my_image");

      image.style.backgroundColor = "#0000ff"; // or "blue" or "rgba(0, 0, 255, 1.0)"

    • with jQuery: 使用jQuery:

      $("#my_image").css({ "background-color" : "#0000ff"});

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM