Remove all the children DOM elements in div

前端 未结 7 2132
萌比男神i
萌比男神i 2020-12-02 08:48

I have the following dojo codes to create a surface graphics element under a div:

....


        
相关标签:
7条回答
  • 2020-12-02 09:30

    First of all you need to create a surface once and keep it somewhere handy. Example:

    var surface = dojox.gfx.createSurface(domNode, widthInPx, heightInPx);
    

    domNode is usually an unadorned <div>, which is used as a placeholder for a surface.

    You can clear everything on the surface in one go (all existing shape objects will be invalidated, don't use them after that):

    surface.clear();
    

    All surface-related functions and methods can be found in the official documentation on dojox.gfx.Surface. Examples of use can be found in dojox/gfx/tests/.

    0 讨论(0)
  • 2020-12-02 09:36

    In Dojo 1.7 or newer, use domConstruct.empty(String|DomNode):

    require(["dojo/dom-construct"], function(domConstruct){
      // Empty node's children byId:
      domConstruct.empty("someId");
    });
    

    In older Dojo, use dojo.empty(String|DomNode) (deprecated at Dojo 1.8):

    dojo.empty( id or DOM node );
    

    Each of these empty methods safely removes all children of the node.

    0 讨论(0)
  • 2020-12-02 09:41

    From the dojo API documentation:

    dojo.html._emptyNode(node);
    
    0 讨论(0)
  • 2020-12-02 09:41

    If you are looking for a modern >1.7 Dojo way of destroying all node's children this is the way:

    // Destroys all domNode's children nodes
    // domNode can be a node or its id:
    domConstruct.empty(domNode);
    

    Safely empty the contents of a DOM element. empty() deletes all children but keeps the node there.

    Check "dom-construct" documentation for more details.

    // Destroys domNode and all it's children
    domConstruct.destroy(domNode);
    

    Destroys a DOM element. destroy() deletes all children and the node itself.

    0 讨论(0)
  • 2020-12-02 09:44
    node.innerHTML = "";
    

    Non-standard, but fast and well supported.

    0 讨论(0)
  • 2020-12-02 09:44
    while(node.firstChild) {
        node.removeChild(node.firstChild);
    }
    
    0 讨论(0)
提交回复
热议问题