将复制的元素的元素复制到数组中而不通过循环遍历它?

huangapple go评论80阅读模式
英文:

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(&quot;*&quot;), and so that they refer to the copied element?

HTML:

&lt;div id=&quot;id1&quot;&gt;&lt;p id=&quot;p1&quot;&gt;1&lt;/p&gt;&lt;p id=&quot;p2&quot;&gt;2&lt;/p&gt;&lt;/div&gt;

JS:

const el = document.getElementById(&quot;id1&quot;);
const arr = [];
if(el)
for (const element of el.getElementsByTagName(&quot;*&quot;)) {
  if(element.textContent === &quot;1&quot;) 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 的子元素来大大简化您的代码。要处理 elnull 的情况,您可以使用一个简单的三元表达式,如果元素不存在,则返回一个空数组:

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(&quot;*&quot;) 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(&#39;id1&#39;);
const arr = el ? [...el.getElementsByTagName(&quot;*&quot;)].filter(x =&gt; x.textContent == &#39;1&#39;) : [];

console.log(arr);

<!-- language: lang-html -->

&lt;div id=&quot;id1&quot;&gt;
  &lt;p id=&quot;p1&quot;&gt;1&lt;/p&gt;
  &lt;p id=&quot;p2&quot;&gt;2&lt;/p&gt;
&lt;/div&gt;

<!-- 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(&#39;id1&#39;);
const arr = el ? [...el.querySelectorAll(&#39;[x-attribute=&quot;1&quot;]&#39;)] : [];

console.log(arr);

<!-- language: lang-html -->

&lt;div id=&quot;id1&quot;&gt;
  &lt;p id=&quot;p1&quot; x-attribute=&quot;1&quot;&gt;First element&lt;/p&gt;
  &lt;p id=&quot;p2&quot; x-attribute=&quot;2&quot;&gt;Second element&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 06:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定