gpt4 book ai didi

javascript - 使用 Javascript 创建的 SVG 路径没有宽度或高度

转载 作者:行者123 更新时间:2023-11-30 11:08:21 25 4
gpt4 key购买 nike

我正在尝试使用 Javascript 和 SVG 以编程方式创建圆 Angular 。拐 Angular 创建成功,但 path 始终设置为 0 宽和 0 高。

如果我复制创建的字符串元素并将其粘贴到一个文件中,那么它可以正常工作,如代码片段的第二行所示。

为什么只有程序创建的路径没有宽度和高度?我错过了什么?

var cornerTopLeft = createCorner("top-left");
applyCornerStyles.call(cornerTopLeft, 0, 0, 10);

var body = document.getElementsByTagName("body")[0];
body.appendChild(cornerTopLeft);

function createCorner(cornerPosition) {
var corner = document.createElement("svg");
corner.setAttribute("fill", "red");
corner.setAttribute("style", "width:10px;height:10px;background-color: red;");
corner.setAttribute("viewBox", "0 0 100 100");
corner.setAttribute("xmlns", "http://www.w3.org/2000/svg")
corner.innerHTML = '<path class="" style="background-color: rgba(0, 0, 0, .5);fill: rgba(0, 0, 0, .5);" d="M100 0 ,Q 0 0 0 100, L0 0, Z"></path>';
return corner;
}

function applyCornerStyles(top, left, size) {
this.style.top = top + "px";
this.style.left = left + "px";
this.style.width = size + "px";
this.style.height = size + "px";
this.style.zIndex = "20002";
this.style.position = "absolute";
}
<h3>This svg was created using svg string created programatcally.</h3>
<svg fill="none" viewbox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" style="top: 22px; left: 510px; width: 10px; height: 10px; z-index: 20001; position: absolute;">
<path class="" style="background-color: rgba(0, 0, 0, .5);fill: rgba(0, 0, 0, .5);" d="M100 0 ,Q 0 0 0 100, L0 0, Z"></path>
</svg>

( fiddle )

最佳答案

为了创建一个新的 svg 元素,您需要使用 document.createElementNS而不是 document.createElement . 您还需要使用 document.createElementNS 创建路径. corner.innerHTML = '<path class=...不会的。

const SVG_NS = 'http://www.w3.org/2000/svg';

var cornerTopLeft = createCorner("top-left");
applyCornerStyles.call(cornerTopLeft, 0, 0, 10);

var body = document.getElementsByTagName("body")[0];
body.appendChild(cornerTopLeft);

function createCorner(cornerPosition) {
var corner = document.createElementNS(SVG_NS, 'svg');
corner.setAttributeNS(null,"fill", "red");
corner.setAttribute("class","corner")
corner.setAttributeNS(null,"viewBox", "0 0 100 100");
//corner.setAttribute("xmlns", "http://www.w3.org/2000/svg")
var path = document.createElementNS(SVG_NS, 'path');
path.setAttributeNS(null,"fill", "rgba(0, 0, 0, .5)");
path.setAttributeNS(null,"d","M100 0 ,Q 0 0 0 100, L0 0, Z");
corner.appendChild(path)
return corner;
}




function applyCornerStyles(top, left, size) {
this.style.top = top + "px";
this.style.left = left + "px";
this.style.width = size + "px";
this.style.height = size + "px";

this.style.position = "absolute";
}
svg{border:1px solid}

.corner{background-color: red;}
<h3>This svg was created using svg string created programatcally.</h3>
<svg fill="none" viewbox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" style="top: 22px; left: 510px; width: 10px; height: 10px; z-index: 20001; position: absolute;">
<path class="" style="background-color: rgba(0, 0, 0, .5);fill: rgba(0, 0, 0, .5);" d="M100 0 ,Q 0 0 0 100, L0 0, Z"></path>
</svg>

关于javascript - 使用 Javascript 创建的 SVG 路径没有宽度或高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54768105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com