jQuery appended table adds closing tag at the end of the text automatically. Why?

前端 未结 4 733
旧时难觅i
旧时难觅i 2020-12-06 14:49
$(\'#all_locations\').append(\"\");
$(\'#all_locations\').append(\"\");

$.each(data, function(i, item){
          
4条回答
  •  不思量自难忘°
    2020-12-06 15:24

    It's best practice to create a string of your HTML to append and run one .append() call on that string:

    //declare our output variable
    var output = '
City
'; //iterate through data $.each(data, function(i, item){ //add to output variable output += ''; } //append the output to the DOM $('#all_locations').append(output);

It's pretty common to see people pushing items into an array and joining that array for the append:

//declare our output variable (array)
var output = ['
City
' + item.city + '
']; //iterate through data $.each(data, function(i, item){ //add to output variable output.push(''); } //append the output to the DOM after joining it together into a string $('#all_locations').append(output.join(''));

提交回复
热议问题
City
' + item.city + '