开发者

javascript appenchild

         for(var i=0; i<myJSONObject.model.length; i++){
         var create_div = document.createElement('div');
         create_div.id = 'model_id'+i;
         create_div.innerHTML = myJSONObject.model[i].model_name;
         var assign_innerHTML = create_div.innerHTML;
         var create_anchor = document.createElement('a');
         document.getElementById('models').appendChild(create_div);
         document.getElementById(create_div.id).appendChild(create_anchor);
     }

for ex the myJSONObject.model.length is 2

the output is like this

<div id = 'model_id0'>XXXXX<a> </a></div>
            <div id = 'model_id1'>XXXXX<a> </a></div> */

but instead of above the output sholud be like this

<div id = model_id0> <a> xxxxxx</a></div> 
<div id = model_id1> <a>开发者_开发问答 xxxxxx</a></div> 

how to append it inside of the innerhtml any one plz reply !!!!


two suggestions:

1.) instead of assigning innerHTML to model_idx div assign the model name to its child a. and 2nd instead of appending it to DOM in every loop do it after completing the loop as to minimize frequent the DOM Update ie by:

objContainer = document.createElement('div');
for(....)
{
     var create_div = document.createElement('div');
     create_div.id = 'model_id'+i;
     var create_anchor = document.createElement('a');
     create_anchor.innerHTML = myJSONObject.model[i].model_name;
     create_div.appendChild(create_anchor);
     objContainer.appendChild(create_div);        
}
document.getElementById('models').appendChild(objContainer);


I would go along the lines of:

var i = 0,
    m = myJSONObject.model,
    l = m.length,
    models = document.getElementById("models");

for(; i < j; i++) {
    var model = m[i];
    var create_div = document.createElement("div");
    create_div.id = "model_id" + i;
    create_div.innerHTML = "<a>" + model.model_name + "</a>";
    models.appendChild(create_div);
}

Unless you specifically need to do something to the anchor itself (other than set its innerHTML), there's no need to create a reference to an element for it. If you do need to do something specific to that anchor, then in that case have this, instead:

EDIT: As per your comment, you DO want to do something to the anchor, so go with this (now updated) option - assuming the anchor will always be a child of the div that has the ID you require. The reason "model_id" + i is being put in as a string is because that is exactly what is being passed into the HTML - the document has no clue what "i" is outside of javascript:

var i = 0,
    m = myJSONObject.model,
    l = m.length,
    models = document.getElementById("models");

for(; i < j; i++) {
    var model = m[i];
    var create_div = document.createElement("div");
    var create_anchor = document.createElement("a");
    create_div.id = "model_id" + i;
    create_anchor.innerHTML = model.model_name;
    if(window.addEventListener) {
        create_anchor.addEventListener("click", function() {
            getModelData(1, this.parentNode.id);
        }, false);
    } else {
        create_anchor.attachEvent("onclick", function() {
            getModelData(1, this.parentNode.id);
        });
    }
    create_div.appendChild(create_anchor);
    models.appendChild(create_div);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜