删除div中的所有子DOM元素 [英] Remove all the children DOM elements in div

查看:362
本文介绍了删除div中的所有子DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following dojo codes to create a surface graphics element under a 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()将首先绘制矩形图形。如果我在这样的锚点href中再次调用这个函数:

drawRec() will draw a rectangle graphics first time. If I call this function again in an anchor href like this:

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

它将再次绘制另一个图形。我需要清理div下的所有图形,然后再次创建。如何添加一些dojo代码呢?

it will draw another graphics again. What I need to clean all the graphics under the div and then create again. How can I add some dojo codes to do that?

推荐答案

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

这篇关于删除div中的所有子DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆