【问题标题】:Can't change the value of a attribute of a SVG Path with Javascript无法使用 Javascript 更改 SVG 路径的属性值
【发布时间】:2020-01-02 12:32:23
【问题描述】:

我使用 snap.svg 创建了一个 SVG 路径,您可以在下面看到它的代码。现在我正在尝试更改属性的值,例如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..

【问题讨论】:

    标签: javascript html svg snap.svg


    【解决方案1】:

    尝试使用.setAttribute()

    L_PUF_WAR.setAttribute("strokeWidth", number);
    

    【讨论】:

    • 也试过了,但后来我收到了这个错误Cannot read property 'attr' of undefined at hallo (index.html:197) at HTMLInputElement.onclick (index.html:203)
    • L_PUF_WAR的范围是什么?
    • 作用域是什么意思?
    • 如果我添加这个。在L_PUF_WAR.strokeWidth = number; 前面,它说它无法在hallo() 处读取未定义的属性'attr'。它应该是一个全局范围
    猜你喜欢
    • 2011-10-12
    • 2020-10-29
    • 2017-07-31
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2021-10-08
    • 2018-12-02
    • 2021-01-12
    相关资源
    最近更新 更多