SVG元素

rocsnake 2002-04-20 08:39:13
加精
怎样动态在SVG的Javascript中动态创建文本元素( <text> </text> )
...全文
80 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2002-04-22
  • 打赏
  • 举报
回复
这里是英文测试,中文测试要注意编码:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg>
<svg width="600" height="500">
<script>
<![CDATA[
var step = 100;
function cree_text(evt)
{
svgdoc=evt.getTarget().getOwnerDocument();
node=svgdoc.createElement("text");
node.setAttribute("x","50");
node.setAttribute("y",step);
node.setAttribute("style","text-anchor:start;font-size:24;font-family:Arial;fill:red");
texte=svgdoc.createTextNode("This Is Added By appendChild.");
node.appendChild(texte);
ou=svgdoc.getElementById("net_lover");
ou.appendChild(node);
step=step+30
}
]]>
</script>
<g id="net_lover"></g>
<text onclick="cree_text(evt);" id="mxh" x="10" y="40" style="font-size:16pt;font-weight:bold" fill="#0099FF">http://colorweb.go.163.com[Click Here Add New Text]</text>
</svg>
rocsnake 2002-04-22
  • 打赏
  • 举报
回复
net_lover(孟子E章)

还是不行,说的是对象不支持此属性与方法(node.text="aaaaaaa";
linhaibo 2002-04-22
  • 打赏
  • 举报
回复
看样子 SVG 没有 VML 方便
孟子E章 2002-04-22
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="500" height="350" onload="alerte(evt)">
<script language="text/ecmascript">
<![CDATA[
var svg;
var node;
function alerte(evt){
svg = evt.getTarget().getOwnerDocument();
node = svg.createElement('text');
node.setAttribute("x","200");
node.setAttribute("y","30");
node.setAttribute("style","text-anchor:left;font-size:12;font-family:Arial;fill:black");
node.text="aaaaaaa";
svg.appendChild(node);
}
]]>
</script>
</svg>
rocsnake 2002-04-22
  • 打赏
  • 举报
回复
不行
linhaibo 2002-04-21
  • 打赏
  • 举报
回复
我想可以这么做的,SVG 也是符合XML的标记语言,是XML的扩展,
<svg id="svg1">
</svg>

var newText=document.createElement("<text></text>");
svg1.insertBefore(newText);
rocsnake 2002-04-20
  • 打赏
  • 举报
回复
不行、这是在HTML中,我问的是SVG中
8988 2002-04-20
  • 打赏
  • 举报
回复
这样可以吗?
str="<text>........</text>"
document.body.insertAdjacentHTML("AfterBegin", str)

87,902

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧