AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

toJSON

概述

toJSON 方法是 ArriveBehavior 类的一个实例方法,用于将传入的 DOM 元素转化为 JSON 格式的数据。

语法

element.toJSON();

返回值

返回值为一个 JSON 格式的数据对象。

示例

<div id="myDiv">
  <p>Hello, World!</p>
</div>
const myDiv = document.getElementById('myDiv');
const json = myDiv.toJSON();
console.log(json);

输出:

{
  "tagName": "DIV",
  "attributes": {
    "id": "myDiv"
  },
  "childNodes": [
    {
      "tagName": "P",
      "attributes": {},
      "childNodes": [
        {
          "nodeType": 3,
          "nodeValue": "Hello, World!"
        }
      ]
    }
  ]
}

属性说明

以下是返回的 JSON 数据对象的属性说明:

  • tagName:元素的标签名,如 "DIV" 或 "P"
  • attributes:元素的属性集合,以键值对的形式表示。如果该元素没有任何属性,则返回一个空对象
  • childNodes:元素的子节点集合,以数组的形式表示。每个子节点也是一个 JSON 对象,其属性有:nodeType 表示节点类型,tagName 表示标签名,attributes 表示属性集合,childNodes 表示子节点集合。如果该元素没有任何子节点,则返回一个空数组

注意事项

  • 该方法只能被调用于一个 DOM 元素上,如果调用者不是一个元素,则会抛出错误
  • 该方法返回的 JSON 数据对象不包含任何函数,仅包含元素的基本信息