研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
固定大小的工作区
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
将 Blockly 添加到网页的最简单方法是将其注入空的“div”标记中。
1. 获取代码
以最适合您的应用的方式获取代码。
2. 定义区域
在页面正文的某个位置添加一个空的 div 并设置其大小:
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
3. 注入工作区
定义工具箱结构:
const toolbox = {
"kind": "flyoutToolbox",
"contents": [
{
"kind": "block",
"type": "controls_if"
},
{
"kind": "block",
"type": "controls_repeat_ext"
},
{
"kind": "block",
"type": "logic_compare"
},
{
"kind": "block",
"type": "math_number"
},
{
"kind": "block",
"type": "math_arithmetic"
},
{
"kind": "block",
"type": "text"
},
{
"kind": "block",
"type": "text_print"
},
]
}
最后,调用以下代码将 Blockly 注入到您定义的 div 中。
const workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
目前未使用 workspace
变量,但以后当想要保存代码块或生成代码时,它会重要。如果在同一页面上注入了多个 Blockly 实例,请确保返回的每个工作区都存储在不同的变量中。
现在,您可以在浏览器中测试该网页。您应该会看到 Blockly 的编辑器填充了 div,工具箱中包含七个块。您可以点击此处查看实时演示。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-05-23。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-05-23。"],[[["Blockly can be easily embedded into a webpage by injecting it into an empty 'div' tag."],["The process involves getting the Blockly code, defining a 'div' element for the workspace, and injecting Blockly with a specified toolbox."],["A workspace variable is returned, which will be important for interacting with the Blockly instance later."]]],["To add Blockly to a webpage, first, obtain the necessary code. Next, create an empty `div` element in the page's body and set its dimensions (e.g., `id=\"blocklyDiv\"`, `height: 480px`, `width: 600px`). Define the toolbox structure using JSON format, specifying the block types. Finally, use `Blockly.inject('blocklyDiv', {toolbox: toolbox})` to insert Blockly into the `div`, storing the result in a `workspace` variable for later use in saving blocks or code generation.\n"]]