Reference Guide
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本節說明如何建構訂閱按鈕的 <div>
元素。下表說明元素的必要和選用屬性。選用屬性可讓你控制按鈕的版面配置和主題,以及選擇是否顯示頻道的訂閱人數。
必要屬性
選用屬性
-
data-layout
:按鈕的格式。有效的屬性值為:
default
:顯示播放按鈕圖示和使用者語言的「訂閱」字樣,使用者語言是根據使用者語言設定或所在位置選取。
full
:除了標準按鈕外,還會顯示頻道的顯示圖片和頻道名稱。
-
data-theme
:指定按鈕要使用的色彩配置。有效值為 default
和 dark
。dark
主題適用於在深色背景元素上放置按鈕的應用程式。
-
data-count
:指出按鈕是否顯示頻道訂閱人數。按鈕的預設行為是顯示訂閱人數。有效值為 default
和 hidden
。
-
data-onytevent
– 注意:這項屬性已淘汰。
事件
注意:訂閱按鈕的 subscribe
和 unsubscribe
事件已淘汰。同樣地,先前可新增至小工具元素的 data-ytonevent
屬性 (用於指定事件通知的監聽器) 也已淘汰。
動態轉譯
除了使用設定工具提供的標準嵌入程式碼,您也可以動態算繪 Subscribe Button。這個方法可避免 API 的 JavaScript 遍歷整個 DOM 來尋找按鈕,進而縮短按鈕的算繪時間。
Subscribe Button 是 Google+ JavaScript API 的一部分,支援標準的 go
和 render
方法,可用於動態算繪訂閱按鈕。舉例來說,您可以使用這些方法,在 DOM ready
事件觸發時,算繪不存在的按鈕,例如以 AJAX 更新的頁面。
方法 |
說明 |
gapi.ytsubscribe.go( opt_container ) |
在指定容器中顯示所有訂閱按鈕。如要顯示的訂閱按鈕元素已存在,請使用這個方法。舉例來說,如果應用程式傳送 AJAX 要求,傳回 Subscribe Button 的完整 <div> 元素,請呼叫 go() 方法來算繪按鈕。
- opt_container
-
包含要顯示的訂閱按鈕的 HTML 元素。指定元素的 ID 或 DOM 元素本身。如果省略這個參數,系統會顯示網頁上的所有訂閱按鈕。
|
gapi.ytsubscribe.render( container, parameters ) |
在指定容器中算繪 Subscribe Button。如果包含 Subscribe Button 的元素尚未存在,且需要建構,請使用這個方法。
- 容器
- 識別要轉譯 Subscribe Button 的空白 HTML 元素。指定元素的 ID 或 DOM 元素本身。
- parameters
- :包含訂閱按鈕屬性的物件,以
key:value 組合表示,例如 {"channel": "GoogleDevelopers", "layout": "full"} 。
|
使用 gapi.ytsubscribe.go 算繪按鈕
下方範例顯示您用來呼叫 gapi.ytsubscribe.go
方法的程式碼,以便在點選連結時動態轉譯按鈕。
注意:當您更新按鈕選項或程式碼時,上述設定工具也會使用 go
方法算繪新按鈕。
使用 gapi.ytsubscribe.render 算繪按鈕
以下範例說明如何呼叫 gapi.ytsubscribe.render
方法,在點選連結時動態算繪按鈕:
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["# Reference Guide\n\nAttributes\n----------\n\nThis section explains how to construct a `\u003cdiv\u003e` element for a subscription button. The lists below explain the element's required and optional attributes. Optional attributes let you control the button's layout and theme as well as choose whether to display the channel's subscriber count.\n\n### Required attributes\n\n- `class` -- Set the value to `g-ytsubscribe`. This class identifies the `\u003cdiv\u003e` element as a container for a subscription button and enables YouTube to dynamically resize the embedded button as explained in the next section.\n\n- You must specify a value for one of the following two attributes:\n\n \u003cbr /\u003e\n\n - `data-channel` -- The name of the channel associated with the button. Sample value: `GoogleDevelopers`.\n - `data-channelid` -- The channel ID associated with the button. Sample value: `UC_x5XG1OV2P6uZZ5FSM9Ttw`. You can retrieve your channel ID in your [YouTube account settings](http://www.youtube.com/account_advanced) or by using the [APIs Explorer](/youtube/youtube_subscribe_button#Retrieve_Channel_ID) at the end of this document. Learn more about [working with channel IDs](/youtube/v3/guides/working_with_channel_ids).\n\n### Optional attributes\n\n- `data-layout` -- The format for the button. Valid attribute values are:\n\n \u003cbr /\u003e\n\n - `default` -- Displays a play button icon and the word 'subscribe' in the user's language, which is selected using either the user's language setting or location.\n - `full` -- Displays the channel's avatar and channel title in addition to the standard button.\n\n \u003cbr /\u003e\n\n- `data-theme` -- Specifies the color scheme to use for the button. Valid values are `default` and `dark`. The `dark` theme is intended for applications that place buttons over a darker background element.\n\n- `data-count` -- Indicates whether the button displays the number of subscribers that the channel has. The button's default behavior is to display the subscriber count. Valid values are `default` and `hidden`.\n\n- `data-onytevent` -- **Note:** This attribute has been deprecated.\n\nEvents\n------\n\n**Note:** The Subscribe Button's `subscribe` and `unsubscribe` events have been deprecated. Similarly, the `data-ytonevent` attribute, which could previously be added to the widget element to specify a listener for event notifications, has also been deprecated.\n\nDynamic rendering\n-----------------\n\nAs an alternative to using the standard embed code from the [configuration tool](#Configure_a_Button), you can dynamically render the Subscribe Button. This approach prevents the API's JavaScript from traversing the entire DOM to locate buttons, which can improve button rendering time.\n\nAs a part of the [Google+ JavaScript API](https://developers.google.com/+/web/api/javascript), the Subscribe Button supports standard `go` and `render` methods, which can be used to dynamically render subscription buttons. For example, you could use these methods to render a button that is not present when the DOM `ready` event fires, such as on a page that is updated with AJAX.\n\n| Method | Description |\n|-----------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `gapi.ytsubscribe.`**go** `(` ` `\u003cvar class=\"notranslate\"\u003eopt_container\u003c/var\u003e `)` | Renders all subscription buttons in the specified container. Use this method if the subscription button elements that you want to render already exist. For example, if your application sends an AJAX request that returns the complete `\u003cdiv\u003e` element for a Subscribe Button, call the `go()` method to render the button. *** ** * ** *** opt_container : The HTML element that contains the subscription buttons to render. Specify either the element's ID or the DOM element itself. If this parameter is omitted, all subscription buttons on the page are rendered. |\n| `gapi.ytsubscribe.`**render** `(` ` `\u003cvar class=\"notranslate\"\u003econtainer\u003c/var\u003e`,` ` `\u003cvar class=\"notranslate\"\u003eparameters\u003c/var\u003e `)` | Renders the Subscribe Button within the specified container. Use this method if the element that will contain the Subscribe Button does not already exist and needs to be constructed. *** ** * ** *** container : Identifies the empty HTML element in which the Subscribe Button will be rendered. Specify either the element's ID or the DOM element itself. parameters : An object containing [subscription button attributes](#Subscription_Button_Attributes) as `key:value` pairs, such as `{\"channel\": \"GoogleDevelopers\", \"layout\": \"full\"}`. |\n\n### Render a button with gapi.ytsubscribe.go\n\nThe example below shows the code that you would use to call the `gapi.ytsubscribe.go` method to dynamically render a button when the link is clicked.\n\n```js\nLink: Click here for a button!\n\nDisplay button here: \n \n\nCode:\n \u003ca href=\"javascript:void(0);\"\n onclick=\"goYtSubscribeButton('GoogleDevelopers')\"\u003eClick here for a button!\u003c/a\u003e\n Display button here: \u003cdiv id=\"yt-button-container-go\"\u003e\u003c/div\u003e\n \u003cscript\u003e\n function goYtSubscribeButton() {\n // ID of element containing \u003cdiv class=\"g-ytsubscribe\"\u003e element(s)\n // The container element already contains the button elements when this\n // function is called.\n var containerId = \"sample-container\";\n gapi.ytsubscribe.go(containerId);\n }\n \u003c/script\u003e\n\n \u003cb\u003eLink\u003c/b\u003e\n \u003ca href=\"javascript:void(0);\"\n onclick=\"goYtSubscribeButton('GoogleDevelopers')\"\u003eClick here for a button\u003c/a\u003e\n\n \u003cb\u003eDisplay button here:\u003c/b\u003e\n \u003cdiv id=\"yt-button-container-go\" style=\"display-inline\"\u003e\u003c/div\u003e\n```\n\n**Note:** The [configuration tool](#Configure_a_Button) above also uses the `go` method to render a new button when you update the button options or code.\n\n### Render a button with gapi.ytsubscribe.render\n\nThe example below demonstrates how to call the `gapi.ytsubscribe.render` method to dynamically render a button when the link is clicked:\n\n```js\nLink: Click here for a button!\n\nDisplay button here: \n \n\nCode:\n \u003ca href=\"javascript:void(0);\"\n onclick=\"renderYtSubscribeButton('GoogleDevelopers')\"\u003eClick here for a button!\u003c/a\u003e\n Display button here: \u003cdiv id=\"yt-button-container-render\"\u003e\u003c/div\u003e\n \u003cscript\u003e\n function renderYtSubscribeButton(channel) {\n var container = document.getElementById('yt-button-container-render');\n var options = {\n 'channel': channel,\n 'layout': 'full'\n };\n gapi.ytsubscribe.render(container, options);\n }\n \u003c/script\u003e\n\n \u003cb\u003eLink:\u003c/b\u003e\n \u003ca href=\"javascript:void(0);\"\n onclick=\"renderYtSubscribeButton('GoogleDevelopers')\"\u003eClick here for a button!\u003c/a\u003e\n\n \u003cb\u003eDisplay button here:\u003c/b\u003e\n \u003cdiv id=\"yt-button-container-render\" style=\"display:inline\"\u003e\u003c/div\u003e\n```"]]