javascript - 如何放置变量而不是文字数字?

标签 javascript html svg literals substitution

我想替换这一行:

 polygon.setAttribute("points", "50,0 200,0 125,150");

它适用于永久号码:

Demo jsFiddle

用这一行:

polygon.setAttributeNS (null, "points", ("LeftWidth,0 RightWidth,0 RopTringleX,RopTringleY"));

jsfiddle Demo

我怎样才能做到这一点?非常感谢。

代码:

function CreateSVG () {
            var xmlns = "http://www.w3.org/2000/svg";
            var boxWidth = 250;
            var boxHeight = 250;
            var LeftWidth=(250-boxHeight)/2;
            var RightWidth=250-LeftWidth;
            var RopTringleX=(RightWidth-(boxWidth/2));
            var RopTringleY=(boxHeight);


            var svgElem = document.createElementNS (xmlns, "svg");
            svgElem.setAttributeNS (null, "viewBox", "0 0 " + boxWidth + " " + boxHeight);
            svgElem.setAttributeNS (null, "width", 250);
            svgElem.setAttributeNS (null, "height", 250);
            svgElem.style.display = "block";

            var polygon = document.createElementNS (xmlns, "polygon");
            svgElem.appendChild (polygon);
alert(RopTringleY);        

            polygon.setAttributeNS (null, "points", ("LeftWidth,0 RightWidth,0 RopTringleX,RopTringleY"));

            var path = document.createElementNS (xmlns, "path");
            path.setAttributeNS (null, 'stroke', "white");
            path.setAttributeNS (null, 'stroke-width', 4);
            path.setAttributeNS (null, 'fill', "yellow");
            polygon.appendChild (path);

            var svgContainer = document.getElementById ("svgTriangle");

            svgContainer.appendChild (svgElem); 
            alert(boxWidth + ' ' + boxHeight);     
        }
CreateSVG ();
​

最佳答案

polygon.setAttribute("points", LeftWidth + ",0 " + RightWidth + ",0 " + RopTringleX + "," + RopTringleY);

关于javascript - 如何放置变量而不是文字数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12198139/

相关文章:

svg - MathJax:如何更改 svg 输出的默认颜色

javascript - javascript 中的 SVG 翻译转换在 IE 11 中不起作用

python - 通过应用变换简化 SVG - 减小尺寸

javascript - 进行更多服务器调用或一次从服务器加载所有数据?

php - 从外部 php 文件更改 innerHTML

html - 使 CSS Grid 适合最小空间

javascript - jQuery 子菜单在折叠时仍然显示下拉菜单

html - 使用下拉菜单中的选项从结果页面下载 CSV 文件

javascript - 删除 body 标签 Angular 应用程序的属性

javascript - Easeljs (createjs) 无法绑定(bind)两个事件监听器