gpt4 book ai didi

jquery 创建的 svg 矩形没有出现

转载 作者:行者123 更新时间:2023-12-01 06:23:12 26 4
gpt4 key购买 nike

我正在尝试使用 jQuery 创建 SVG 条形图,但没有使用其他库,但是当我创建矩形时,它们不会显示在浏览器中,尽管在 html 中输入的相同矩形确实会显示。

var data = [4, 8, 15, 16, 23, 42];
var chart = jQuery("#chart");
jQuery.each(data, function(index, value) {
var bar = jQuery("<rect/>");
chart.append(bar);
bar.attr("y", index * 20)
.attr("width", value * 10)
.attr("height", 20);
});

和 html:

<svg id="chart" class="chart" width=420 height=120 style="display: block;">
<rect y="0" width="40" height="20"></rect> <!--added to test-->
</svg>

输入的 rect 标记看起来很好,并且使用 Chrome 中的开发人员工具,它看起来与我的代码生成的第一个标记相同,但代码生成的标记不会显示。

最佳答案

$(document).ready(function() {
var data = [4, 8, 15, 16, 23, 42];
var chart = jQuery("#chart2");
jQuery.each(data, function(index, value) {
var bar = $(document.createElementNS("http://www.w3.org/2000/svg", "rect")).attr({
x: 0,
y: index * 20,
width: value * 10,
height: 20,
stroke: "red",
fill: "white"
});
chart.append(bar);
});
});​

http://jsfiddle.net/26ybf/

编辑:显然您可以删除描边和填充属性,我只是将其放在那里以确保其正常工作。干杯!

关于jquery 创建的 svg 矩形没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13732326/

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