英文:
How to copy the elements of the copied element into an array without looping through it?
问题
I'm providing the translated content as requested:
在不通过再次使用 getElementsByTagName(""*")
迭代节点的情况下,你能告诉我如何将元素复制到数组中,以便它们引用已复制的元素吗?
HTML:
<div id="id1"><p id="p1">1</p><p id="p2">2</p></div>
JS:
const el = document.getElementById("id1");
const arr = [];
if (el)
for (const element of el.getElementsByTagName("p")) {
if (element.textContent === "1") arr.push(element)
}
const cloneEl = el?.cloneNode(true);
function arrCloneWithRelationToCloneEl(arr, cloneEl){
...
return arrClone;
}
结果:
arrClone = [p#p1]
从 cloneEl
复制的。是否可以在不迭代克隆元素的节点的情况下完成这个操作,而是通过子节点中的 ID 或创建虚拟 DOM 来完成呢?也许有一些特殊的复制函数,只需不必每次克隆对象时都进行循环。
非常感谢你的答案!
我尝试通过在 childNodes 数组中创建路径来实现它。类似于结果中的 [0,0]
数组。
英文:
Can you please tell me how to copy elements to an array without going through them again through getElementsByTagName("*")
, and so that they refer to the copied element?
HTML:
<div id="id1"><p id="p1">1</p><p id="p2">2</p></div>
JS:
const el = document.getElementById("id1");
const arr = [];
if(el)
for (const element of el.getElementsByTagName("*")) {
if(element.textContent === "1") arr.push(element)
}
const cloneEl = el?.cloneNode(true);
function arrCloneWithRelationToCloneEl(arr , cloneEl){
...
return arrClone;
}
Result:
arrClone = [p#p1]
from cloneEl.
Can this be done at all without iterating over the nodes of the cloned element, but somehow by id in childNodes or by creating a virtual DOM to do it? Maybe there is some special function for copying, only to not have to loop every time an object is cloned.
I will be very grateful for the answer!
I tried to do it through creating path in childNodes array. Something like this array [0,0]
in results.
答案1
得分: 1
Sure, here is the translated code:
首先,使用 document.getElementById
不会克隆节点。它将元素作为 DOM API 的对象获取。
对于您的用例,通过获取具有特定 .textContent
的子元素,没有任何可以用来获取元素的函数。但是,您可以通过使用 扩展语法 将 el.getElementsByTagName("*")
转换为数组,然后使用 .filter()
仅获取具有指定 .textContent
的子元素来大大简化您的代码。要处理 el
为 null
的情况,您可以使用一个简单的三元表达式,如果元素不存在,则返回一个空数组:
const el = document.getElementById('id1');
const arr = el ? [...el.getElementsByTagName("*")].filter(x => x.textContent == '1') : [];
console.log(arr);
但是,如果您要查找具有某个属性、类别或任何可以使用 CSS 选择器搜索的其他内容,那么可以使用 el.querySelectorAll
,再次使用扩展语法将其转换为数组:
const el = document.getElementById('id1');
const arr = el ? [...el.querySelectorAll('[x-attribute="1"]')] : [];
console.log(arr);
希望这对您有所帮助。
英文:
First, using document.getElementById
doesn't clone the node. It gets the element as an object of the DOM API.
For your use case by getting a child element with a specific .textContent
, there isn't any function you can use to get the element. However, you can shorten your code by a lot by converting el.getElementsByTagName("*")
into an array using spread syntax, then using .filter()
to only get sub-elements with the specified .textContent
. To handle el
being null
, you can use a quick ternary expression to return an empty array if the element doesn't exist:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const el = document.getElementById('id1');
const arr = el ? [...el.getElementsByTagName("*")].filter(x => x.textContent == '1') : [];
console.log(arr);
<!-- language: lang-html -->
<div id="id1">
<p id="p1">1</p>
<p id="p2">2</p>
</div>
<!-- end snippet -->
However, if you're instead looking for an element with a certain attribute, class, or anything else that can be searched for with a CSS selector, then you can use el.querySelectorAll
and once again use spread syntax to convert to an array:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const el = document.getElementById('id1');
const arr = el ? [...el.querySelectorAll('[x-attribute="1"]')] : [];
console.log(arr);
<!-- language: lang-html -->
<div id="id1">
<p id="p1" x-attribute="1">First element</p>
<p id="p2" x-attribute="2">Second element</p>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论