研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
标签字段
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
标签字段会将字符串存储为 value
,并将字符串存储为 text
。标签字段的 value
和 text
始终相同。
标签字段

收起的版块上的标签字段

创建
JSON
{
"type": "example_label",
"message0": "a label %1 and another label",
"args0": [
{
"type": "input_dummy"
}
]
}
插值参数之间的任何消息文本都会变为标签字符串。或者,您也可以明确插入标签,以对象或文本的形式插入。通常不建议这样做,因为这会增加翻译难度。
{
"type": "example_label",
"message0": "%1 %2 %3",
"args0": [
{
"type": "field_label",
"text": "a label"
},
{
"type": "input_dummy"
},
"and another label"
]
}
JavaScript
Blockly.Blocks['example_label'] = {
init: function() {
this.appendDummyInput()
.appendField(new Blockly.FieldLabel('a label'));
this.appendDummyInput()
.appendField('and another label');
}
};
appendField 函数接受 FieldLabel
对象,也接受字符串(更常见)来创建标签。
label 字段接受一个可选值和一个可选的 CSS 类字符串。两者均默认为空字符串。
序列化
标签字段不可序列化。
如果您希望标签进行序列化(因为它会通过编程方式进行更改),请参阅可序列化标签字段。
验证器
标签字段不支持验证器,因为它们不可修改。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eA label field displays text on a block and stores the same string as both its \u003ccode\u003evalue\u003c/code\u003e and \u003ccode\u003etext\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eLabel fields can be created using JSON by specifying message strings or by explicitly interpolating labels, though the latter is discouraged for translation reasons.\u003c/p\u003e\n"],["\u003cp\u003eIn JavaScript, label fields are created using \u003ccode\u003eBlockly.FieldLabel\u003c/code\u003e and appended to block inputs using \u003ccode\u003eappendField\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eWhile not serializable by default, a separate "Serializable Label" field exists for cases requiring programmatic changes and serialization.\u003c/p\u003e\n"],["\u003cp\u003eLabel fields do not support validators due to their non-editable nature.\u003c/p\u003e\n"]]],[],null,["# Label fields\n\nA label field stores a string as its `value` and a string as its `text`. The\n`value` and `text` of a label field are always the same.\n\n#### Label field\n\n#### Label field on collapsed block\n\nCreation\n--------\n\n### JSON\n\n {\n \"type\": \"example_label\",\n \"message0\": \"a label %1 and another label\",\n \"args0\": [\n {\n \"type\": \"input_dummy\"\n }\n ]\n }\n\nAny message text between interpolation arguments becomes label strings.\nAlternatively, labels may be interpolated explicitly, either as an object or\nas text. This is generally discouraged as it makes translation more\ndifficult. \n\n {\n \"type\": \"example_label\",\n \"message0\": \"%1 %2 %3\",\n \"args0\": [\n {\n \"type\": \"field_label\",\n \"text\": \"a label\"\n },\n {\n \"type\": \"input_dummy\"\n },\n \"and another label\"\n ]\n }\n\n### JavaScript\n\n Blockly.Blocks['example_label'] = {\n init: function() {\n this.appendDummyInput()\n .appendField(new Blockly.FieldLabel('a label'));\n this.appendDummyInput()\n .appendField('and another label');\n }\n };\n\nThe [appendField](/blockly/reference/js/Blockly.Input#appendField)\nfunction accepts both `FieldLabel` objects and, more commonly, strings to create\nlabels.\n\nThe label field takes in an optional value, and an optional css class string.\nBoth default to an empty string.\n\nSerialization\n-------------\n\nLabel fields are not serializable.\n\nIf you would like your label to be serialized, because it is being changed\nprogrammatically, see the [Serializable Label](/blockly/guides/create-custom-blocks/fields/built-in-fields/label-serializable)\nfield.\n\nValidators\n----------\n\nLabel fields do not support validators, because they are not editable."]]