gpt4 book ai didi

javascript - JavaScript 生成的 SVG 中的外部对象

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

我正在尝试在使用 javascript 动态创建的 SVG 中创建一个简单文本输入数组。

首先我是用 PHP 做的,代码片段看起来像这样:

for ($row=0; $row<7; $row++)
{
for ($col=0; $col<7; $col++)
{
$fx=130+100*$col;
$fy=120+100*$row;
echo "<foreignObject x="".$fx."" y="".$fy."" width="50" height="80">\n<body xmlns="http://www.w3.org/1999/xhtml">\n<form><input type="text" width="1"/ style="font-size:48px; border:none;"></form>\n</body>\n</foreignObject>";
}
}

然后在 javascript 中,我会尝试像这样的单个实例,但我没有让它工作。

var field = document.createElementNS("http://www.w3.org/2000/svg", "foreignObject");
field.setAttribute("x", "130");
field.setAttribute("y", "120");
field.setAttribute("width", "50");
field.setAttribute("height", "80");
mySvg.appendChild(field.cloneNode(true));
var s = '<body xmlns="http://www.w3.org/1999/xhtml"><form><input type="text" width="1" style="font-size:48px; border:none;"></form></body>';
var s1 = document.createElement(s);
field.appendChild(s1);
</script>

有什么提示吗?或者我应该切换到完全不同的实现(例如 CSS)?

最佳答案

document.createElement 需要一个元素名称作为参数并创建单个元素,它不会解析任意 html/xml 内容。

您可以使用 DOMParser解析一串标记。

至于 PHP,您可能需要 escape回显中的内部双引号。

关于javascript - JavaScript 生成的 SVG 中的外部对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13763947/

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