Blockly 将于 2025 年 11 月 10 日迁移到 Raspberry Pi Foundation!阅读博客和常见问题解答。
 
  
         
       
     
  
  
  
    
  
  
  
    
      创建工作区
    
    
      
    
    
      
      使用集合让一切井井有条
    
    
      
      根据您的偏好保存内容并对其进行分类。
    
  
  
      
    
  
  
  
  
  
    
  
  
    
    
    
Blockly 工作区是 Blockly 的最高级别组件。您可以使用此界面通过代码块进行编程。
如需详细了解工作区及其子组件,请参阅可视化术语表。
注入 div
必须将 Blockly 工作区注入到 <div> 中,该 <div> 称为“注入 div”。
注入 div 的大小可以静态或动态调整。div 中的 Blockly 元素会在窗口调整大小时更新其大小。
以下代码段展示了静态大小的注入 div 的 HTML:
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
注射
注入会创建构成工作区界面的所有 HTML 子元素。它还会执行所有必要的初始化操作,以使工作区准备就绪,可供使用。
注入函数可以接受注入 div 的 ID,也可以接受注入 div 本身:
// Passes the ID.
const workspace = Blockly.inject('blocklyDiv', { /* config */ });
// Passes the injection div.
const workspace = Blockly.inject(
    document.getElementById('blocklyDiv'), { /* config */ });
配置
在注入期间,可以使用多种选项(例如布局和样式)配置工作区。
如需详细了解配置选项,请参阅配置选项。
  
  
  
 
  
    
    
      
       
    
    
  
  
  如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
  最后更新时间 (UTC):2025-09-11。
  
  
  
    
      [null,null,["最后更新时间 (UTC):2025-09-11。"],[],["Blockly workspaces, the UI for block programming, are injected into a designated `\u003cdiv\u003e` element called the \"injection div.\" This div can have a fixed or dynamic size, with Blockly elements resizing accordingly. Injection initializes the workspace UI and readies it for use. The `Blockly.inject()` function, which creates the workspace, accepts either the injection div's ID or the div itself.  Workspace configuration, including layout and style, is set during the injection process.\n"]]