gpt4 book ai didi

javascript - 使用 JavaScript 创建元素时,SVG 折线不会显示点

转载 作者:行者123 更新时间:2023-11-28 14:28:32 25 4
gpt4 key购买 nike

我需要使用 SVG/折线元素动态创建迷你图,使用纯 HTML 的示例完美地工作,当我使用 JavaScript 创建元素并添加属性时出现问题。

创建元素的函数

    function createElement(type, attributes, someElement) {
var element = type == "svg" ? document.createElementNS('http://www.w3.org/2000/svg', 'svg') : document.createElement(type);
for (var key in attributes) {
if (key === "class") {
var cls = attributes[key];
for (var c in cls)
element.classList.add(cls[c]);
} else {
element[key] = attributes[key];
}
}
someElement.appendChild(element);
}

在这里,我创建了 SVG 元素并将其添加到名为 filter_r_inner 的 div 中,然后添加属性。

                    var newElement = createElement("svg", {
"class": ['mktcap_spark'],
"id": "weekly_svg",
"viewBox": "0 0 500 100"
}, filter_r_inner);
var weekly_svg = document.getElementById("weekly_svg");
weekly_svg.setAttribute("viewBox", "0 0 500 100");

在这里,我创建了折线元素并将其添加到 SVG 元素,然后添加属性。

                    var newElement = createElement("polyline", {
"id": "weekly_poly"
}, weekly_svg);
var weekly_poly = document.getElementById("weekly_poly");
weekly_poly.setAttribute('points', "00,120 20,60 40,120 60,10 80,80 100,80 120,60 140,100 160,90 180,80 200, 110 220, 10 240, 70 260, 100 280, 100 300, 40 320, 0 340, 100 360, 100 380, 120 400, 60 420, 70 440, 80 460, 20 480, 50 500, 30");
weekly_poly.setAttribute("fill", "none");
weekly_poly.setAttribute("stroke", "#e9be3d");
weekly_poly.setAttribute("stroke-width", "8");

上面的内容没有像我预期的那样渲染 SVG 迷你图,但是添加了所有属性,但没有显示任何内容。

我也尝试过以这种方式从 this 添加点问题,这给了我一个 Weekly_svg.points is undefined 的错误

            var point = weekly_svg.createSVGPoint();
point.x = 10;
point.y = 20;
weekly_poly.points.appendItem(point);

我也研究过 setAttributeNS 但它需要一个“命名空间”,我尝试了这个但仍然没有显示。

        weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', 'points', "00,120 20,60 40,120 60,10 80,80 100,80 120,60 140,100 160,90 180,80 200, 110 220, 10 240, 70 260, 100 280, 100 300, 40 320, 0 340, 100 360, 100 380, 120 400, 60 420, 70 440, 80 460, 20 480, 50 500, 30");
weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', "fill", "none");
weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', "stroke", "#e9be3d");
weekly_poly.setAttributeNS('http://www.w3.org/2000/svg', "stroke-width", "8");

这里的示例与纯 HTML 完美配合。

<svg viewBox="0 0 500 100" class="mktcap_spark">
<polyline
fill="none"
stroke="#e9be3d"
stroke-width="8"
points="
00,120
20,60
40,120
60,10
80,80
100,80
120,60
140,100
160,90
180,80
200, 110
220, 10
240, 70
260, 100
280, 100
300, 40
320, 0
340, 100
360, 100
380, 120
400, 60
420, 70
440, 80
460, 20
480, 50
500, 30
"
/>

</svg>

它呈现出如下所示的迷你图

enter image description here

CSS

.mktcap_spark {

  width: 130px;
height: 50px;
min-width: 130px;
}

最佳答案

这里我想到了一些事情。第一个是您创建了一个与已存在的函数相匹配的函数。不完全是一个可靠的想法 - 它会崩溃,你会在未来的某个时候哭泣。

接下来是您使用createElement的(非svg)常规dom方法 - 呃呃,在使用svgs时不能做任何事情,您需要使用createElementNS函数。

利用一些旧代码和你的代码,我想出了如下所示的代码:

window.addEventListener('load', onDocLoad, false);

function onDocLoad(evt) {
document.body.appendChild(makeSVG(svgData));
}


var svgData = [{
type: 'svg',
data: {
viewBox: "0 0 500 100"
}
},
{
type: 'polyline',
data: {
fill: "none",
stroke: "#e9be3d",
strokeWidth: "8",
points: "00,120 20,60 40,120 60,10 80,80 100,80 120,60 140,100"
}
},
];


function getNode(n, v) {
n = document.createElementNS("http://www.w3.org/2000/svg", n);
for (var p in v) {
n.setAttributeNS(null, p.replace(/[A-Z]/g, function(m, p, o, s) {
return "-" + m.toLowerCase();
}), v[p]);
}
return n
}

function makeSVG(data) {
var result;
data.forEach(
function(elem, index, array) {
if (index)
result.appendChild(getNode(elem.type, elem.data));
else
result = getNode(elem.type, elem.data);
}
);
return result;
}

关于javascript - 使用 JavaScript 创建元素时,SVG 折线不会显示点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52390409/

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