Blockly 已於 2025 年 11 月 10 日移交給 Raspberry Pi Foundation!請參閱網誌文章和常見問題。
Google uses AI technology to translate content into your preferred language. AI translations can contain errors.
圖片欄位
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
圖片欄位會將字串儲存為值,並將字串儲存為文字。值是圖片的 src,而文字則是描述/代表圖片的 alt 字串。
圖片欄位

摺疊區塊中的圖片欄位

創作
JSON
{
"type": "example_image",
"message0": "image: %1",
"args0": [
{
"type": "field_image",
"src": "https://www.gstatic.com/codesite/ph/images/star_on.gif",
"width": 15,
"height": 15,
"alt": "*"
}
]
}
JavaScript
Blockly.Blocks['example_image'] = {
init: function() {
this.appendDummyInput()
.appendField("image:")
.appendField(new Blockly.FieldImage(
"https://www.gstatic.com/codesite/ph/images/star_on.gif",
15,
15,
"*"));
}
};
圖片建構函式會接收下列項目:
| 參數 |
說明 |
src |
指向點陣圖檔案的字串。 |
width |
必須轉換為非零數字。 |
height |
必須轉換為非零數字。 |
opt_alt |
(選用) 準確描述/代表圖片的字串。當區塊摺疊時,系統會改用這張圖片。如果是 null 或 undefined,則會使用空字串。 |
opt_onClick |
(選用) 點選欄位時要呼叫的函式。 |
opt_flipRtl |
(選用) 布林值。 如果是 true,圖片會在從右到左模式下沿垂直軸翻轉。預設值為 false。 適用於「向左轉」和「向右轉」圖示。 |
序列化
圖片欄位無法序列化。
點擊處理常式
圖片欄位不接受驗證器,而是明確接受在點選欄位時呼叫的函式。也就是說,圖片可以像區塊上的按鈕一樣運作。
您可以在 JavaScript 建構函式中設定點按事件處理常式,也可以使用 setOnClickHandler 函式設定。
以下是點擊處理常式的範例,可在呼叫時收合區塊。
function() {
this.getSourceBlock().setCollapsed(true);
}

除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2026-02-18 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間:2026-02-18 (世界標準時間)。"],[],["An image field's value is the image's `src` URL, and its text is the `alt` string. Key actions include: specifying the `src`, `width`, `height`, and optional `alt` and `onClick` parameters during creation. The `onClick` function enables images to act as buttons. The `opt_flipRtl` parameter is used to mirror the image. The field is constructed through JSON or JavaScript. Image fields are not serializable. The `setOnClickHandler` function can also be used to set the `onClick` function.\n"]]