在此頁面上:瞭解專案選取器在視覺化體驗撰寫器 (VEC)中的運作方式、如何選擇使用的選取器,以及Target如何產生專案的選取器。
元素選取器是可識別一或多個元素的CSS運算式。 有關CSS選取器的基本資訊可在Mozilla Developer Network (MDN)上的選取器檔案中取得。
是否使用元素類別或元素ID是在帳戶偏好設定中設定。 按一下管理 > 視覺化體驗撰寫器,然後選擇偏好的CSS選取器:
- 使用元素ID:如果多個元素使用相同的ID,或如果頁面載入時元素ID可能會變更,則停用。
- 使用專案類別:如果頁面上的專案類別可能變更,則停用。
- 使用偏好的選取器:啟用以在VEC中使用唯一的選取器來識別網站的關鍵區域。
如需何時使用CSS選取器以及何時使用唯一ID的相關資訊,請參閱視覺化體驗撰寫器最佳作法和限制。
Target如何產生專案的選取器 generate-selector
Target使用簡單的演演算法來建立選取器。 產生邏輯如下:
-
如果專案具有ID (例如
id="container"),則專案的選取器是#container。例如:
code language-html <div class="wrapper"> <div id="container"> <!-- Selector is computed for this element --> <ul class="navigation"> <li class="item active"> Home </li> <li class="item"> Men </li> <li class="item"> Women </li> <li class="item"> Kids </li> </ul> </div> </div> -
如果專案包含類別屬性,Target會嘗試使用專案上任何類別的第一類別。
Target會嘗試剖析父元素,直到找到
<HTML>元素或具有ID的元素為止。 每當元素包含ID且選取器在其子繫上計算時,此元素的ID即會貢獻給選取器。例如:
code language-html <div class="wrapper"> <div id="container"> <!-- id is present here. It contributes to selector --> <ul class="navigation"> <li class="item active"> Home </li> <!-- Selector is computed for this element --> <li class="item"> Men </li> <li class="item"> Women </li> <li class="item"> Kids </li> </ul> </div> </div>在此範例中,選取器是
#container>ul.navigation:eq(0)>li.item:eq(0)(「>」表示直屬子項)。eq提供具有「tagName=UL」和第一個類別navigation的專案的索引。 因此,index為 0。 如需詳細資訊,請參閱 MDN 中的選取器文章。 -
如果專案不含類別,Target會針對專案使用
tagName,並往父專案周遊,直到找到<HTML>專案或具有ID的專案為止。例如:
code language-html <div class="wrapper"> <div id="container"> <!-- id is present here. It contributes to selector --> <ul class="navigation"> <li> Home </li> <li> Men </li> <li class="active"> Women </li> <li> Kids </li><!-- Selector is computed for this element --> </ul> </div> </div>選取器是
#container>ul.navigation:eq(0)>li:nth-of-type(4)。
在此程式中:
- 只要能唯一識別DOM中的元素,可使用任何CSS選取器。
- 以上方法是Target使用的方法。 Target未強制採用此方法。 只要選取器可唯一識別元素,即可新增選取器。
- 選擇器中可使用任何屬性。 本文僅使用類別名稱作為範例。
This section contains structured knowledge intended to support interpretation, retrieval, and question answering related to this topic.
For complete understanding, this information should be combined with the documentation on this page. Neither source is intended to stand alone; the page describes the feature, while this section provides additional context that helps disambiguate terminology, intent, applicability, and constraints.
- TL;DR: This page explains how element selectors work in the Visual Experience Composer (VEC), how to choose which selectors are used, and how Target generates a selector for an element.
Intents:
- Understand what an element selector is in the Visual Experience Composer (VEC).
- Choose which CSS selectors are used, through Administration > Visual Experience Composer in the account preferences.
- Decide when to disable Use element IDs or Use element classes.
- Understand how Target generates a selector for an element that has an ID, a class, or no class.
- Understand which selectors can be used: any CSS selector that uniquely identifies an element, and any attribute in the selector.
Glossary:
- Element selector: a CSS expression that can identify one or more elements.
- Use preferred selectors: a setting in Administration > Visual Experience Composer that, when enabled, uses unique selectors in the VEC to identify key areas of the website. (product-specific)
Applicability:
- Element classes are available as selectors in A/B Test, Automated Personalization, and Multivariate Test activities.
Terminology:
- Canonical name: Visual Experience Composer
- Acronym: VEC (Visual Experience Composer)
FAQ:
- Where do I choose whether element classes or element IDs are used? In the account preferences: click Administration > Visual Experience Composer, then choose the preferred CSS selectors.
- When should I disable Use element IDs? Disable it if the same ID is used for multiple elements, or if element IDs might change on page load.
- When should I disable Use element classes? Disable it if element classes on a page might change.
- Does Target require its own selector generation approach? No. Target does not mandate the approach described on the page; any selector can be added, as long as it uniquely identifies the element.
- Which attributes can be used in a selector? Any attribute can be used in the selector. The page uses only a class name as an example.
Examples: (each one shows how Target generates a selector for an element)
- Element with an ID: if an element has an ID, for example
id="container", the selector for the element is#container. - Element with a class attribute: Target attempts to use the first class of any classes present on the element, and attempts to parse the parent element until it finds the
<HTML>element or an element with an ID. Whenever an element contains an ID and the selector is computed on its descendant child, the ID of this element contributes to the selector. For the firstliwith the classesitem activeinsideul class="navigation"insidediv id="container", the selector is#container>ul.navigation:eq(0)>li.item:eq(0)(" > " indicates the immediate child).eqgives the index of the element that has “tagName=UL” and the first classnavigation, soindexis 0. - Element without a class: Target uses
tagNamefor the element and traverses up the parent element until either the<HTML>element or an element with an ID is found. For the fourthli(without a class) insideul class="navigation"insidediv id="container", the selector is#container>ul.navigation:eq(0)>li:nth-of-type(4).
Related pages:
- When to use CSS selectors and when to use unique IDs: Visual Experience Composer best practices and limitations (c-experiences/c-visual-experience-composer/experience-composer-best-practices.md)
Personas:
- admin: Can choose which CSS selectors are used in the account preferences, by selecting Administration > Visual Experience Composer. Can disable Use element IDs if the same ID is used for multiple elements, or if element IDs might change on page load. Can disable Use element classes if element classes on a page might change. Where: “FAQ”.
- developer: Can add any selector, as long as it uniquely identifies the element, and can use any attribute in the selector. The page states that Target does not mandate the approach described on the page and uses only a class name as an example. Where: “FAQ”.
Onboarding:
- Level: Experienced
- First minimal step: Select Administration > Visual Experience Composer in the account preferences, then choose the preferred CSS selectors.
- Next: Learn when to use CSS selectors and when to use unique IDs: Visual Experience Composer best practices and limitations (c-experiences/c-visual-experience-composer/experience-composer-best-practices.md)