jQuery 是否有对应于js的createDocumentFragment方法

2025-01-02 04:42:44
推荐回答(1个)
回答1:

//不建议的做法
var browserList = ["Internet Explorer", "Mozilla Firefox", "Safari", "Chrome", "Opera"];
$.each(browserList, function (index,value) {
$('

  • ').text(value).appendTo($('ul').eq(0));
    })

    //好的做法NO1
    var browserList = ["Internet Explorer", "Mozilla Firefox", "Safari", "Chrome", "Opera"];
    var myHTML = '';
    $.each(browserList, function (index, value) {
    myHTML += '
  • ' + value + '
  • ';
    })
    $('ul').eq(0).html(myHTML);

    //好的作法NO2
    var frag = document.createDocumentFragment();
    var browserList = ["Internet Explorer", "Mozilla Firefox", "Safari", "Chrome", "Opera"];
    $.each(browserList, function (index, value) {
    var li = document.createElement("li");
    li.textContent = value;
    frag.appendChild(li);
    })
    $('ul').eq(0).append($(frag));