Node节点对象

本文深入探讨了DOM中的Node节点对象,重点关注nodeName属性用于获取节点名称,nodeType揭示节点类型,以及nodeValue如何表示节点的值。通过对这三个关键属性的理解,开发者能更好地操作和遍历DOM树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nodeName 节点名称
nodeType 节点类型
nodeValue 节点的值

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
    <input type="text" id="nameId" value="zhangsan" />
    <span id="spanId">我是span区域</span>
</body>
<script type="text/javascript">
/*
            元素          属性          文本
nodeName    大写的标签名  属性名称        #text
nodeType    固定的1        固定的2        固定的3
nodeValue   null        属性的值        内容

parentNode  获取父节点
childNodes  所有子节点
firstChild  第一个子节点
lastChild   最后一个子节点
*/
    var input = document.getElementById("nameId");
        alert(input.nodeName); //INPUT
        alert(input.nodeType); //1
        alert(input.nodeValue); //null
    var attr = input.getAttributeNode("type");
        alert(attr.nodeName); //type
        alert(attr.nodeType); //2
        alert(attr.nodeValue); //text
    var span = document.getElementById("spanId");
    var text = span.firstChild;
        alert(text.nodeName); //#text
        alert(text.nodeType); //3
        alert(text.nodeValue); //我是span区域

</script>
</html>
// NodePool.ts import { _decorator, Node, Pool, Prefab, instantiate } from 'cc'; const { ccclass } = _decorator; @ccclass('NodePool') export class NodePool { // 存储池对象的数组 private _pool: Node[] = []; // 关联的预制体 private _prefab: Prefab; // 池名称(用于调试) private _name: string; /** * 构造函数 * @param prefab 预制体资源 * @param name 对象池名称(可选) * @param initialSize 初始大小(可选) */ constructor(prefab: Prefab, name: string = "NodePool", initialSize: number = 0) { this._prefab = prefab; this._name = name; // 初始化对象池 this.resize(initialSize); } /** * 调整对象池大小 * @param size 目标大小 */ resize(size: number): void { const currentSize = this._pool.length; if (size > currentSize) { // 增加池大小 for (let i = currentSize; i < size; i++) { const node = instantiate(this._prefab); node.active = false; this._pool.push(node); } } else if (size < currentSize) { // 减小池大小 for (let i = currentSize - 1; i >= size; i--) { const node = this._pool.pop(); if (node && node.isValid) { node.destroy(); } } } } /** * 从对象池中获取节点 * @returns 节点对象 */ get(): Node { if (this._pool.length > 0) { const node = this._pool.pop()!; node.active = true; return node; } else { // 池为空时创建新节点 const node = instantiate(this._prefab); node.active = true; return node; } } /** * 将节点放回对象池 * @param node 节点对象 */ put(node: Node): void { if (!node || !node.isValid) return; // 重置节点状态 node.active = false; node.removeFromParent(); node.position = new Vec3(0, 0, 0); node.scale = new Vec3(1, 1, 1); // 放回池中 this._pool.push(node); } /** * 清空对象池并销毁所有节点 */ clear(): void { for (let i = 0; i < this._pool.length; i++) { const node = this._pool[i]; if (node && node.isValid) { node.destroy(); } } this._pool = []; } /** * 获取当前对象池中的对象数量 */ size(): number { return this._pool.length; } /** * 获取对象池名称 */ get name(): string { return this._name; } } 在GameManager中合理使用NodePool
最新发布
08-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值