无法使用Javascript更改SVG路径的属性值

时间:2019-08-29 09:01:44

标签: javascript html svg snap.svg

我使用snap.svg创建了SVG路径,下面可以看到它的代码。现在,我尝试cdhange属性的值,例如strokeWidth。为此,我创建了一个文本输入框和一个运行函数的按钮,该函数应为strokeWidth填充一个新值。但这行不通。我究竟做错了什么?我是否可能甚至无法更改值?我还发布了输入框和按钮的代码,以及下面的更改笔触宽度的功能

我尝试了很多不同的功能和脚本,但是似乎都没有用。有时我收到错误消息,有时没有。当我收到错误消息时,它总是说未定义函数或未定义L_PUF_WAR。

var L_PUF_WAR = s.path("m 937.88689,516.10127 v -51.8831 H 573.37482 V 340.49692 H 391.11879 V 276.6408").attr({
                        fill: "none",
                        stroke: "#bada55",
                        strokeWidth: 15,
                        strokeDasharray: "30, 30 ,30"
                    });
                    A_PUF.append(L_PUF_WAR);```       

    <script>
        function hallo(){
            var number = document.getElementById("number").value;
            if(number > 0){
                L_PUF_WAR.strokeWidth = number;
            }
        }
    </script>
        <input type="text" id="number" name="number"/><br/>  
        <input type="button" value="breite" onclick="hallo()"/> 

I am expecting to put in a value for the strokewidth into the Input Box for example 20 and then the strokeWidth should change to that value..

1 个答案:

答案 0 :(得分:0)

尝试使用.setAttribute()

L_PUF_WAR.setAttribute("strokeWidth", number);