将字符串转换为DOM节点

更新时间:2023-06-29 08:57:28 阅读: 评论:0

将字符串转换为DOM节点
My original post featured DOMParr, a JavaScript API for converting HTML strings into DOM nodes. While DOMParr works well in most cas, that API does have some rough edges and isn't as performant as another API: ContextualFragment. I've rewritten this post to highlight ContextualFragment, but if you still care to learn about DOMParr, plea e the original text at the bottom of this post.
我的原始⽂章介绍了DOMParr ,这是⼀个JavaScript API,⽤于将HTML字符串转换为DOM节点。 尽管DOMParr在⼤多数情况下都能很好地⼯作,但该API确实有⼀些粗糙之处,并且不如另⼀个API: ContextualFragment性能强。 我将这篇⽂章改写为突出显
⽰ContextualFragment ,但是如果您仍然想了解DOMParr ,请参阅本⽂底部的原始⽂本。
It wasn't too long ago that browrs were mostly stagnant when it came to implementing new APIs and features, leading to the ri of MooTools (FTW), jQuery, Dojo Toolkit, Prototype, and likewi JavaScript toolkits. Then we started doing more client side rendering and were forced to u a variety of tricks to handle templates, including massive HTML strings in our JavaScript and even abusing <script> tags to hold our templates.
不久之前,浏览器在实现新的API和功能时⼤多停滞不前,从⽽导致MooTools(FTW),jQuery,Dojo Toolkit,Prototype和JavaScript ⼯具包的兴起。 然后,我们开始做更多的客户端渲染,并被迫使⽤各种技巧来处理模板,包括JavaScript中的⼤量HTML字符串,甚⾄滥⽤<script>标签来保存模板。
Of cour after you've placed your content into the template, you then need to turn that string into DOM nodes, and that process had a few of its own tricks, like creating an offscreen, dummy <div>, tting its innerHTML to the string value, grabbing the firstChild, and moving the node to its desired node. Each JavaScript toolkit would u its own strategy for converting string to DOM, highlighting the need for a standard method to accomplish this task.
当然,在将内容放⼊模板后,您需要将该字符串转换为DOM节点,并且该过程有⼀些⾃⼰的技巧,例如创建屏幕外的虚拟<div> ,将
其innerHTML设置为字符串值,获取firstChild ,然后将节点移动到所需的节点。 每个JavaScript⼯具箱都将使⽤其⾃⼰的策略将字符串转换为DOM,从⽽强调需要⼀种标准⽅法来完成此任务。
Today there's a little known (but standard) way for converting string to DOM with JavaScript: ContextualFragment.
今天,有⼀种鲜为⼈知(但标准)的⽅法,可以使⽤JavaScript将字符串转换为DOM: ContextualFragment 。
脸水肿怎么办
I've touched on for performance in the past, but that post illustrated element creation ateElement:
我过去曾接触过以提⾼性能,但是该⽂章通过ateElement说明了元素创建:
// U a DocumentFragment to store and then mass inject a list of DOM nodes
彩裙鱼是染色的吗
var frag = ateDocumentFragment();
for(var x = 0; x < 10; x++) {
var li = ateElement("li");
li.innerHTML = "List item " + x;
frag.appendChild(li);
}
To create DOM nodes from a string of HTML we'll ateRange().createContextualFragment:
为了从HTML字符串创建DOM节点,我们将使⽤ateRange().createContextualFragment :
let frag = ateRange().createContextualFragment('
One
Two
祛湿吃什么食物');
console.log(frag);
/*
#document-fragment
One
五瓣花
Two
*/
DocumentFragment objects share most of the methods that NodeList objects have, so you can u typical DOM methods like querySelector and querySelectorAll as well DOM traversal properties like firstChild with the resulting DocumentFragment:
DocumentFragment对象共享NodeList对象拥有的⼤多数⽅法,因此您可以使⽤典型的DOM⽅法(例如querySelector和querySelectorAll )以及DOM遍历属性(例如firstChild与⽣成的DocumentFragment :
let firstChild = frag.firstChild;
let firstDiv = frag.querySelector('div');
let allDivs = frag.querySelectorAll('div');
When you're ready to inject all of the created DOM nodes, you can simply execute:
当准备注⼊所有创建的DOM节点时,只需执⾏以下命令即可:
/
/ "placementNode" will be the parent of the nodes within the DocumentFragment
placementNode.appendChild(frag);
You can also inject nodes one at a time:
吕祖庙您也可以⼀次注⼊⼀个节点:
placementNode.appendChild(frag.firstChild);平行四边形的面积公式
ateRange().createContextualFragment function is an awesome, sane method for converting strings to DOM nodes within JavaScript. Ditch your old shims and switch to this performant, simple API!
索性
原始帖⼦: DOMParr (The Original Post: DOMParr)
Today we have a standard way for converting string to DOM with JavaScript: DOMParr.
今天,我们有了使⽤JavaScript将字符串转换为DOM的标准⽅法: DOMParr 。
JavaScript (The JavaScript)
All you need to do is create a DOMParr instance and u its parFromString method:
您需要做的就是创建⼀个DOMParr实例并使⽤其parFromString⽅法:
干虾怎么做好吃
let doc = new DOMParr().parFromString('<div><b>Hello!</b></div>', 'text/html');
let doc = new DOMParr().parFromString('<div><b>Hello!</b></div>', 'text/html');
Returned is a document containing the nodes generated from your string. With said document you can u standard node traversal methods to retrieve the nodes we specified in our string:
返回的是包含从字符串⽣成的节点的document 。 通过上述document您可以使⽤标准节点遍历⽅法来检索我们在字符串中指定的节点:
let doc = new DOMParr().parFromString('<div><b>Hello!</b></div>', 'text/html');
let div = doc.body.firstChild;
let divs = doc.body.querySelectorAll('div');
You don't need a single wrapping element like JSX components -- you can have sibling elements:
您不需要像JSX组件那样的单个包装元素-您可以拥有同级元素:
let doc = new DOMParr().parFromString('<div>1</div><div>2</div>', 'text/html');
let firstDiv = doc.body.firstChild;
let condDiv = Sibling;
let doc = new DOMParr().parFromString('<div>1</div><div>2</div>', 'text/html');
let firstDiv = doc.body.firstChild;
let condDiv = Sibling;
Here's a simple wrapping function for DOMParr to retrieve the nodes:
这是DOMParr检索节点的简单包装函数:
let getNodes = str => new DOMParr().parFromString(str, 'text/html').body.childNodes;
let nodes = getNodes('<div>1</div><div>2</div>');
// [div, div]
The DOMParr object is an awesome, sane method for converting strings to DOM nodes within JavaScript. Ditch your old shims and switch to this efficient, simple API!
DOMParr对象是⼀种很棒的,理智的⽅法,⽤于将字符串转换为JavaScript中的DOM节点。 抛弃旧的垫⽚,并切换到此⾼效,简单的API!

本文发布于:2023-06-29 08:57:28,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1065289.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:节点   字符串   创建   转换   模板
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图