如何在js中设置svg路径样式



我有一个路径,这是svg的效果

<path
   style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
   d="m 117.14283,292.36218 c 6.4302,40.00204 17.7957,72.08138 42.8572,97.14285 41.9864,41.98645 170.1489,-58.42248 108.5714,-120 -34.4011,-34.4011 -81.0249,123.66456 -65.7143,154.28572 20.1806,40.36118 166.194,14.76546 134.2857,-17.14286 -49.3132,-49.31326 -75.8868,85.36926 -60,117.14286 30.4479,60.89583 115.0776,-13.49389 97.1429,-31.42857 -3.4884,-3.48837 -113.508,91.67283 -22.8572,157.14285"
   id="path3024"
   inkscape:connector-curvature="0" /> 

我在js代码中选择它,像这样

var path = svg.select("#" + options["pathid"]);

那么我如何改变描边不透明度值呢?

这个方法行不通=(

var path = svg.select("#" + options["pathid"])
      .style('stroke', '#ff0000');

=======================================================

path.style("stroke-opacity", 0); works and

alert(path.style("stroke-opacity")); // returns 0, 

但是当我在浏览器中启动svg时没有任何变化,并且标记在代码中根本没有改变。什么好主意吗?

使用JavaScript,您可以轻松地更改属性。

path.setAttribute('stroke','red');
path.setAttribute('stroke-width','2');

如果svg已经被d3选中,那么访问该路径可以使用:

var path = svg.select("#path3024")

尝试:

.attr('stroke', '#ff0000');

最新更新