研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
JavaScript 中的块结构
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档将讨论如何使用 JavaScript 定义代码块中的输入和字段(包括标签)。如果您不熟悉这些术语,请先参阅块的结构,然后再继续。
您还可以以 JSON 格式定义输入、字段和连接。
JavaScript API 针对每种输入类型都包含一个 append
方法:
JavaScript
init: function() {
// ...
this.appendEndRowInput()
.appendField('for each')
.appendField('item')
.appendField(new Blockly.FieldVariable(), 'VAR');
this.appendValueInput('LIST')
.setCheck('Array')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField('in list');
this.appendStatementInput('DO')
.appendField('do');
this.appendDummyInput()
.appendField('end');
}
每个 appendInput
方法都可以接受一个标识符字符串,代码生成器使用该字符串来检索与输入连接的块的代码。代码生成器很少引用虚拟输入和行尾输入,因此通常没有理由为其分配标识符。
JavaScript API 还包含一个用于附加自定义输入的通用 appendInput
方法。请注意,在这种情况下,标识符应直接传递给自定义输入的构造函数。
JavaScript
init: function() {
// ...
this.appendInput(new MyCustomInput('INPUT_NAME'))
.appendField('an example label')
}
所有 appendInput
方法(包括泛型和非泛型)都会返回输入对象,以便可以使用方法链进一步配置这些对象。有三种用于配置输入的内置方法。
附加字段
创建输入并使用 appendInput
将其附加到块后,可以选择性地将任意数量的字段附加到输入。这些字段通常用作标签,用于描述每个输入的用途。
JavaScript
init: function() {
// ...
this.appendDummyInput()
.appendField('hello');
}

最简单的字段是标签。Blockly 的惯例是使用全小写文本,专有名词(例如 Google、SQL)除外。
输入行可以包含任意数量的字段。多个 appendField
调用可以链接在一起,以便高效地向同一输入行添加多个字段。
JavaScript
init: function() {
// ...
this.appendDummyInput()
.appendField('hello')
.appendField(new Blockly.FieldLabel('Neil', 'person'));
}

appendField('hello')
调用实际上是使用显式 FieldLabel
构造函数(即 appendField(new Blockly.FieldLabel('hello'))
)的快捷方式。只有在指定类名时,才需要使用构造函数,这样才能使用 CSS 规则设置标签的样式。
连接检查
JavaScript
init: function() {
// ...
this.appendValueInput('NUM')
.setCheck('Number');
}
setCheck
方法用于检查连接的类型。如果给定实参为 null(默认值),则此输入可连接到任何块。如需了解详情,请参阅连接检查。
对齐字段
JavaScript
init: function() {
// ...
this.appendValueInput('LIST')
.appendField('in list')
.setAlign(Blockly.inputs.Align.RIGHT);
}
setAlign
方法用于对齐输入中的字段。有三个自描述值可作为实参传递给此函数:Blockly.inputs.Align.LEFT
、Blockly.inputs.Align.RIGHT
和 Blockly.inputs.Align.CENTER
。
当块以从右到左的模式(例如阿拉伯语和希伯来语)呈现时,左侧和右侧会反转。因此,Blockly.inputs.Align.RIGHT
会将字段向左对齐。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[],[],null,["# Block structure in JavaScript\n\nIn this document, we'll discuss how to use JavaScript to define the inputs and\nfields (including labels) in your block. If you're not familiar with these\nterms, see [Anatomy of a\nblock](/blockly/guides/create-custom-blocks/define/block-anatomy) before\nproceeding.\n\nYou can also define your inputs, fields, and connections [in\nJSON](/blockly/guides/create-custom-blocks/define/structure-json).\n\nAppend inputs\n-------------\n\nThe JavaScript API includes an `append` method for each [input\ntype](/blockly/guides/create-custom-blocks/define/block-anatomy#inputs): \n\n### JavaScript\n\n init: function() {\n // ...\n this.appendEndRowInput()\n .appendField('for each')\n .appendField('item')\n .appendField(new Blockly.FieldVariable(), 'VAR');\n this.appendValueInput('LIST')\n .setCheck('Array')\n .setAlign(Blockly.inputs.Align.RIGHT)\n .appendField('in list');\n this.appendStatementInput('DO')\n .appendField('do');\n this.appendDummyInput()\n .appendField('end');\n }\n\nEach `appendInput` method can take an identifier string, which is used by code\ngenerators to retrieve code for the block connected to the input. Code\ngenerators rarely reference dummy and end-of-row inputs, so there is generally\nno reason to assign them an identifier.\n\nThe JavaScript API also includes a generic `appendInput` method for appending\n[custom inputs](/blockly/guides/create-custom-blocks/inputs/creating-custom-inputs). Note that in this case, the identifier should\nbe passed directly to your custom input's constructor. \n\n### JavaScript\n\n init: function() {\n // ...\n this.appendInput(new MyCustomInput('INPUT_NAME'))\n .appendField('an example label')\n }\n\nAll of the `appendInput` methods (both generic and non-generic) return the\ninput object so that they can be further configured using method chaining. There\nare three built-in methods used for configuring inputs.\n\nAppend fields\n-------------\n\nOnce an input has been created and appended to a block with `appendInput`, one\nmay optionally append any number of\n[fields](/blockly/guides/create-custom-blocks/define/block-anatomy#fields) to\nthe input. These fields\nare often used as labels to describe what each input is for. \n\n### JavaScript\n\n init: function() {\n // ...\n this.appendDummyInput()\n .appendField('hello');\n }\n\nThe simplest field is a label. Blockly's convention is to use all\nlowercase text, with the exception of proper names (e.g. Google, SQL).\n\nAn input row can contain any number of fields. Multiple `appendField`\ncalls may be chained together to efficiently add several fields to the same\ninput row. \n\n### JavaScript\n\n init: function() {\n // ...\n this.appendDummyInput()\n .appendField('hello')\n .appendField(new Blockly.FieldLabel('Neil', 'person'));\n }\n\nThe `appendField('hello')` call is actually a shortcut for using an explicit\n`FieldLabel` constructor: `appendField(new Blockly.FieldLabel('hello'))`.\nThe only time one would wish to use the constructor is when specifying a\nclass name so that the label may be styled using a CSS rule.\n\nConnection checks\n-----------------\n\n### JavaScript\n\n init: function() {\n // ...\n this.appendValueInput('NUM')\n .setCheck('Number');\n }\n\nThe `setCheck` method is used for type-checking connections. If given\nan argument of null, the default, then this input may be connected to any block.\nSee [Connection\nchecks](/blockly/guides/create-custom-blocks/inputs/connection-checks) for\ndetails.\n\nAlign fields\n------------\n\n### JavaScript\n\n init: function() {\n // ...\n this.appendValueInput('LIST')\n .appendField('in list')\n .setAlign(Blockly.inputs.Align.RIGHT);\n }\n\nThe `setAlign` method is used to align fields within an input. There are three\nself-descriptive values which may be passed as an argument to this function:\n`Blockly.inputs.Align.LEFT`, `Blockly.inputs.Align.RIGHT`, and\n`Blockly.inputs.Align.CENTER`.\n\nWhen a block is rendered in right-to-left mode (e.g. Arabic and Hebrew), left\nand right are reversed. Thus `Blockly.inputs.Align.RIGHT` would align fields to\nthe left."]]