gpt4 book ai didi

javascript - 删除 div 中的所有子 DOM 元素

转载 作者:IT王子 更新时间:2023-10-29 02:38:54 26 4
gpt4 key购买 nike

我有以下 dojo 代码来在 div 下创建表面图形元素:

....
<script type=text/javascript>
....
function drawRec(){
var node = dojo.byId("surface");
// remove all the children graphics
var surface = dojox.gfx.createSurface(node, 600, 600);

surface.createLine({
x1 : 0,
y1 : 0,
x2 : 600,
y2 : 600
}).setStroke("black");
}
....
</script>
....
<body>
<div id="surface"></div>
....

drawRec() 会第一次绘制一个矩形图形。如果我像这样在 anchor href 中再次调用此函数:

 <a href="javascript:drawRec();">...</a>

它会再次绘制另一个图形。我需要的是把div下的所有图形都清理干净,然后重新创建。我怎样才能添加一些道场代码来做到这一点?

最佳答案

while (node.hasChildNodes()) {
node.removeChild(node.lastChild);
}

关于javascript - 删除 div 中的所有子 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/683366/

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