Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

javascript - 使用jQuery创建HTML元素的最有效方法是什么?(What is the most efficient way to create HTML elements using jQuery?)

Recently I've been doing a lot of modal window pop-ups and what not, for which I used jQuery.

(最近,我一直在做很多模态窗口弹出窗口,而我没有使用jQuery。)

The method that I used to create the new elements on the page has overwhelmingly been along the lines of:

(我用来在页面上创建新元素的方法绝大多数遵循以下原则:)

$("<div></div>");

However, I'm getting the feeling that this isn't the best or the most efficient method of doing this.

(但是,我感到这不是执行此操作的最佳或最有效的方法。)

What is the best way to create elements in jQuery from a performance perspective?

(从性能的角度来看,在jQuery中创建元素的最佳方法是什么?)

This answer has the benchmarks to the suggestions below.

(该答案具有以下建议的基准。)

  ask by Darko Z translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I use $(document.createElement('div'));

(我用$(document.createElement('div'));)

Benchmarking shows this technique is the fastest.

(基准测试表明该技术是最快的。)

I speculate this is because jQuery doesn't have to identify it as an element and create the element itself.

(我推测这是因为jQuery不必将其标识为元素并创建元素本身。)

You should really run benchmarks with different Javascript engines and weigh your audience with the results.

(您应该真正使用不同的Javascript引擎运行基准测试,并用结果权衡受众。)

Make a decision from there.

(从那里做出决定。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...