gpt4 book ai didi

javascript - html javascript node.children 似乎是一个指针

转载 作者:行者123 更新时间:2023-12-03 01:37:07 25 4
gpt4 key购买 nike

在编写一些 svg 代码时,我遇到了一些让我惊讶的事情。

let Children = node.children 中,变量 children - 似乎充当指向节点子级列表的指针,而不是存储子级列表的变量它被定义的时间。经过研究后,常规 HTML 元素似乎也会发生这种情况。

这是逻辑行为还是错误?

以下示例详细说明了这一点:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SVG Children Bug ?</title>
</head>
<body>
<svg id="mainGrid">
<circle cx="100" cy="75" r="60" stroke="black" stroke-width="3" fill="red" />
<path id="path"
d="m70,52 v48 h36 a24,24 0 0 0 0,-48 z m0,16 h-20 m0,16 h20 m60,-8 h20"
stroke="black"
fill="white"/>
<line x1="0" y1="0" x2="200" y2="150" style="stroke:rgb(255,0,0);stroke-width:2"
</svg>
<div id="mainDiv">
<div></div>
</div>
<script type="text/javascript">
// Svg check children
let svg = document.getElementById('mainGrid');
let Shildren = svg.children;

console.log( "Svg children len: ", Shildren.length ); // Should be 3

let newGroup = document.createElementNS('http://www.w3.org/2000/svg','g');
newGroup.setAttribute('class','newNode');
svg.appendChild( newGroup );

console.log( "Svg children len after adding: ", Shildren.length ); // Should be 3 not 4

// Div check children

let div = document.getElementById('mainDiv');
let Dhildren = div.children;

console.log( "Svg children len: ", Dhildren.length ); // Should be 1

let newDiv = document.createElement('div');
newDiv.setAttribute('class','newDiv');
div.appendChild( newDiv );

console.log("Div children len after adding: ", Dhildren.length ); // Should be 1 not 2
</script>
</body>
</html>

有没有一种简单的方法可以获取特定时间的 child 列表 - 或者我必须在相关时刻将它们一一复制?

最佳答案

正如 MDN 所说:

The ParentNode property children is a read-only property that returns a live HTMLCollection which contains all of the child elements of the node upon which it was called.

它是实时的,这意味着它可以随着子元素的添加或删除而改变,这不是那么直观并且很难处理。一个简单的方法是,每当 live 部分出现问题时,将子级转换为数组,以确保您拥有的内容是静态的:

let Shildren = [...svg.children];

// Svg check children
let svg = document.getElementById('mainGrid');
let Shildren = [...svg.children];

console.log("Svg children len: ", Shildren.length); // Should be 3

let newGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
newGroup.setAttribute('class', 'newNode');
svg.appendChild(newGroup);

console.log("Svg children len after adding: ", Shildren.length); // Should be 3 not 4

// Div check children

let div = document.getElementById('mainDiv');
let Dhildren = [...div.children];

console.log("Svg children len: ", Dhildren.length); // Should be 1

let newDiv = document.createElement('div');
newDiv.setAttribute('class', 'newDiv');
div.appendChild(newDiv);

console.log("Div children len after adding: ", Dhildren.length); // Should be 1 not 2
<svg id="mainGrid">
<circle cx="100" cy="75" r="60" stroke="black" stroke-width="3" fill="red" />
<path id="path"
d="m70,52 v48 h36 a24,24 0 0 0 0,-48 z m0,16 h-20 m0,16 h20 m60,-8 h20"
stroke="black"
fill="white"/>
<line x1="0" y1="0" x2="200" y2="150" style="stroke:rgb(255,0,0);stroke-width:2"
</svg>
<div id="mainDiv">
<div></div>
</div>

关于javascript - html javascript node.children 似乎是一个指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51018243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com