Javascript Rendering HTML with “.append” - Simple

2019-07-26 07:10发布

问题:

Can Anyone explain what each line of this code means? I'm trying to move the <script> call statement to a specific position in my HTML but the code generated by my script always moves this to the bottom of one particular table and I think it has something to do with this code. I would prefer to position it myself by rewriting this function to insert the code at the point where the <script> appears in the HTML.

function initialize(instance) {
        _this = instance;
        // build html
        var realCaller = caller != null? caller:$("body");
        var cornerClass = options.showRoundCorner? "ui-corner-all ":"";
        realCaller.append("<div id='"+windowId+"' class='window_panel "+cornerClass+options.containerClass+"'></div>");
        container = realCaller.children("div#"+windowId);

Thanks in advance!

回答1:

Your <script> tag should be left wherever it was originally. Moving it around will not help.

I am assuming the page is using jQuery, but I can't tell for sure from what you've posted.

First, you need to put an id attribute on the page element where you want the HTML to be inserted (i.e. <div id="your_id_here"></div>. Then, change this line:

var realCaller = caller != null? caller:$("body");

To this:

var realCaller = $('#your_id_here');

Replacing "your_id_here" with the ID that you set on the page element.

Note: Since you posted so little of your code, it's hard to know whether this "fix" will break other parts of the code....



回答2:

function initialize(instance) {
        _this = instance; //  _this is now equal to the instance variable.
        // build html
        var realCaller = caller != null? caller:$("body"); // if caller isn't null make realCaller equal to caller otherwise the body element
        var cornerClass = options.showRoundCorner? "ui-corner-all ":""; // same deal here
        realCaller.append("<div id='"+windowId+"' class='window_panel  "+cornerClass+options.containerClass+"'></div>"); // append this element to corner class variable
        container = realCaller.children("div#"+windowId); // container equals all children within the realcaller object.