原生javascript使用innerHTML来追加元素

时间:2017-04-27 01:51:54

标签: javascript

大家。

我遇到性能更差的问题。我制作表并希望插入超过5000行,因此我选择jquery append()来插入表。

jquery with append()

  let nativeTable = $("#mainScrollPage");
  let allhtml = "";

  for (var key =0;key<5100;key++) {
    var headName = "<td class='tableData '>a</td>";
    var htmlReceive = "<td class='tableData'>b</td>";
    var htmlRead = "<td class='tableData'>c</td>";
    var htmlResearch = "<td class='tableData'>d</td>";
    var htmlVoice = "<td class='tableData'>e</td>";
    var html = "<tr class='tableRow'>" + headName + htmlReceive + htmlRead + htmlResearch + htmlVoice + "</tr>";
    allhtml += html;
  }

  nativeTable.append(allhtml);

append()在笔记本电脑上工作很棒。但是在移动设备中,当移动设备呈现为桌面时会出现延迟。所以我找到了原生的插入方式。

原生Javascript

  let nativeTable = document.getElementById("mainScrollPage");
  let allhtml = "";

  for (var key =0;key<5100;key++) {
    var headName = "<td class='tableData '>a</td>";
    var htmlReceive = "<td class='tableData'>b</td>";
    var htmlRead = "<td class='tableData'>c</td>";
    var htmlResearch = "<td class='tableData'>d</td>";
    var htmlVoice = "<td class='tableData'>e</td>";
    var html = "<tr class='tableRow'>" + headName + htmlReceive + htmlRead + htmlResearch + htmlVoice + "</tr>";
    allhtml += html;
  }

  nativeTable.innerHTML = (allhtml);

我使用text-tag而不是创建document.createelement,因为console.time直接使用标记比创建附加到DOM的元素更快。

不幸的是,它比jquery的性能更好,它仍然在nativeJS中有所滞后。 设备为Xiaomi 2iphone 6s。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

试一试 HTML:

- (void)downloadImageAttachment:(CBLDocument *)doc 
  attachmentName:(NSString *)attachmentName 
  completionBlock:(downloadCompletionBlock) block
{
self.attachmentDocumentID = doc.documentID;
CBLAttachment *attachment = [doc.currentRevision attachmentNamed:attachmentName];
self.attachmentName = attachmentName;
self.completionBlock = block;

self.progress = [self.pullData downloadAttachment: attachment];
[self.pullData start];
[self.progress addObserver:self forKeyPath: @"fractionCompleted" options: NSKeyValueObservingOptionNew context: NULL];
[self.progress.userInfo addObserver: self forKeyPath: kCBLProgressErrorKey options: NSKeyValueObservingOptionNew context: NULL];
}

Javascript:

[attriString setAttributedString:[NSAttributedString new]];