【问题标题】:Modify SVG color without using CSS or modifying SVG file在不使用 CSS 或修改 SVG 文件的情况下修改 SVG 颜色
【发布时间】:2018-07-25 21:35:13
【问题描述】:

有没有人在不使用 CSS 或直接修改文件的情况下更改 svgicon 的颜色/属性的任何提示/知识?

例如:

const style = {
  somestyle: {
    random style configs
  }
}

<svgIcon className={somestyle} />

是否可以在不使用 CSS 或直接修改文件的情况下将该 svgIcon 更改为与默认颜色不同的颜色?

或者也许使用和修改 matieral-ui 提供的 svg 会更容易?

【问题讨论】:

标签: svg material-ui


【解决方案1】:

您可以使用 javascript 来修改各个 SVG 元素的样式。

<html>
<head>
    <script>
      function setColor(color)
      {
        document.getElementById("rect").style.fill = color;
      }
    </script>
</head>
<body>
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" style="width: 300px; height: 250px">
    <rect x="50" y="20" rx="10" ry="10" width="200" height="200" style="fill:red; stroke:black; stroke-width:3" id="rect" />
</svg>

  <button onclick="setColor('red');">Red</button>
  <button onclick="setColor('green');">Green</button>
  <button onclick="setColor('blue');">Blue</button>
</body>
</html>

http://jsfiddle.net/VDzGG/

【讨论】:

  • 所以我必须将 svg 放在修改它的同一页面上才能修改其颜色?假设我从其中的库中导入 svg,颜色变化可能不会影响它吧?
猜你喜欢
  • 2021-05-03
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 2022-01-01
  • 2015-09-08
  • 2022-11-17
相关资源
最近更新 更多