简体   繁体   English

使用前置和追加动态创建DOM结构

[英]Creating dynamically DOM structure with prepend and append

I would like to create dynamically (with jQuery) the following structure : 我想动态创建(使用jQuery)以下结构:

      <div id="mainWindow">
        <div id="webgl">
          <div id="global-ui">
            <div id="gui"></div>
            <div id="buttons-wrapper">  
            <div id="buttons">
            <button type="button" id="startButtonId" class="btn btn-primary btn-xs" tabindex="13">Start</button>
            <button type="button" id="resetButtonId" class="btn btn-default btn-xs" tabindex="14">Reset</button>
            <button type="submit" id="saveButonId" class="btn btn-primary btn-xs">Save to file</button> 
            </div>
            </div>
            </div>
         </div>
       </div> 

Here what I have done (with popID = 'mainWindow' and HTML code already contains " <div id="mainWindow"></div> ") : 在这里,我所做的(使用popID = 'mainWindow'并且HTML代码已经包含“ <div id="mainWindow"></div> ”):

// Initialization of component objects                                               
    webGlDiv = document.createElement('div');                                            
    webGlDiv.id = 'webgl';                                                               
    $('#'+popID).prepend(webGlDiv);                                                      
    globalUiDiv = document.createElement('div');                                         
    globalUiDiv.id = 'global-ui';                                                        
    $('#'+webGlDiv.id).prepend(globalUiDiv);                                             
    guiDiv = document.createElement('div');                                              
    guiDiv.id = 'gui';                                                                   
    $('#'+globalUiDiv.id).append(guiDiv);                                                
    buttonsWrapperDiv = document.createElement('div');                                   
    buttonsWrapperDiv.id = 'buttons-wrapper';                                            
    $('#'+globalUiDiv.id).prepend(buttonsWrapperDiv);                                    
    buttonsDiv = document.createElement('div');                                          
    buttonsDiv.id = 'buttons';                                                           
    $('#'+buttonsWrapperDiv.id).prepend(buttonsDiv);                                     
    startButton.id = 'startButtonId';                                                    
    $('#'+buttonsDiv.id).prepend(startButton);                                              
    resetButton.id = 'resetButtonId';                                                    
    $('#'+buttonsDiv.id).append(resetButton);                                            
    saveButton.id = 'saveButtonId';                                                      
    $('#'+buttonsDiv.id).append(saveButton);    

and I get the following structure with this just above : 我在上面得到以下结构:

DOM动态创建

I have difficulties to insert ("opening" and "closing" tags) elements (like <div id="gui"></div> ) 我很难插入(“ opening”和“ closing”标签)元素(例如<div id="gui"></div>

I have noticed that append() adds ("opening" and "closing") tags whereas prepend() just seems to add opening tag at the start : I don't know how to handle this difference. 我注意到, append()添加了(“ opening”和“ closing”)标签,而prepend()似乎只是在开始时添加了open标签:我不知道如何处理这种差异。

Could you help me please to fix it and create the HTML structure wanted at the beginning of my question ? 您能帮我解决这个问题,并在问题开始时创建所需的HTML结构吗?

Thanks for your help 谢谢你的帮助

Change: 更改:

$('#'+globalUiDiv.id).prepend(buttonsWrapperDiv);

to

$('#'+globalUiDiv.id).append(buttonsWrapperDiv);

The order is wrong because you appended the gui first, meaning that when you prepend the button div, it will become the first element, which means gui is pushed to the second row. 顺序是错误的,因为你的附加gui第一,这意味着当你prepend按钮DIV,它将成为第一要素,这意味着GUI是被推到了第二排。

 // Initialization of component objects var popID = "mainWindow"; webGlDiv = document.createElement('div'); webGlDiv.id = 'webgl'; $('#'+popID).prepend(webGlDiv); globalUiDiv = document.createElement('div'); globalUiDiv.id = 'global-ui'; $('#'+webGlDiv.id).prepend(globalUiDiv); guiDiv = document.createElement('div'); guiDiv.id = 'gui'; $('#'+globalUiDiv.id).append(guiDiv); buttonsWrapperDiv = document.createElement('div'); buttonsWrapperDiv.id = 'buttons-wrapper'; $('#'+globalUiDiv.id).append(buttonsWrapperDiv); buttonsDiv = document.createElement('div'); buttonsDiv.id = 'buttons'; $('#'+buttonsWrapperDiv.id).prepend(buttonsDiv); startButton = document.createElement("button"); startButton.id = 'startButtonId'; $('#'+buttons.id).prepend(startButton); resetButton = document.createElement("button"); resetButton.id = 'resetButtonId'; $('#'+buttonsDiv.id).append(resetButton); saveButton = document.createElement("button"); saveButton.id = 'saveButtonId'; $('#'+buttonsDiv.id).append(saveButton); console.log($('#'+popID)[0].outerHTML); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="mainWindow"> </div> 

Jquery was invented to make your life easier. 发明了Jquery可以使您的生活更轻松。 But you continue to complicate it with native javascript) 但是您继续使用本地javascript使其复杂化)

$(document).ready(function() {
    var html = 
        '<div id="webgl">'+
          '<div id="global-ui">'+
            '<div id="gui"></div>'+
            '<div id="buttons-wrapper">'+
            '<div id="buttons">'+
            '<button type="button" id="startButtonId" class="btn btn-primary btn-xs" tabindex="13">Start</button>'+
            '<button type="button" id="resetButtonId" class="btn btn-default btn-xs" tabindex="14">Reset</button>'+
            '<button type="submit" id="saveButonId" class="btn btn-primary btn-xs">Save to file</button>'+
            '</div>'+
            '</div>'+
            '</div>'+
         '</div>';
    var popID = 'mainWindow';
    $('#'+popID).append(html);

});

Different than other answers here is how I do things with Jquery. 与其他答案不同的是,我如何使用Jquery进行操作。 First I have a function to create dom elements. 首先,我具有创建dom元素的功能。

(function ($) {

    $.fn.createAndAppend = function (type, attr, style) {
        var element = document.createElement(type);
        if (this != null)
            this.append(element);
        for (var i in attr) {
            element[i] = attr[i];
            //element.setAttribute(i, attr[i]);
        }
        for (var i in style) {
            element.style[i] = style[i];
        }
        return $(element);
    };

})(jQuery);

you can use it like: 您可以像这样使用它:

var body = $("body");
var webgl = body.createAndAppend("div",{id:"webgl"});
var globalui = webgl.createAndAppend("div",{id:"global-ui"});

.... ....

when you want to add buttons 当您想添加按钮时

var myButton = globalui.createAndAppend("button",{id:"myPrettyButton", innerHTML:"myButton", className:"myButtonClass"},{"width":"150px","height":"50px"})

As you can see, you can set inner html, class, id and any style you want. 如您所见,您可以设置内部html,class,id和所需的任何样式。 I like this way because I can do all the things in a single line. 我喜欢这种方式,因为我可以在一行中完成所有操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM