Selettori di elementi utilizzati nel Compositore esperienza visivo vec-selectors

In questa pagina: Scopri come funzionano i selettori di elementi nel Compositore esperienza visivo (VEC), come scegliere quali selettori utilizzare e come Target genera un selettore per un elemento.

Un selettore di elementi è un’espressione CSS che può identificare uno o più elementi. Le informazioni di base sui selettori CSS sono disponibili nel documento Selettori in Mozilla Developer Network (MDN).

L’utilizzo delle classi di elementi o degli ID di elementi è impostato nelle preferenze dell’account. Fai clic su Amministrazione > Compositore esperienza visivo, quindi scegli i selettori CSS preferiti:

  • Usa ID elemento: disabilita se lo stesso ID viene utilizzato per più elementi o se gli ID degli elementi potrebbero cambiare al caricamento della pagina.
  • Usa classi elemento: disabilita se le classi elemento in una pagina potrebbero cambiare.
  • Usa selettori preferiti: abilita l’utilizzo di selettori univoci nel Compositore esperienza visivo per identificare aree chiave del sito Web.
NOTE
Le classi di elementi sono disponibili come selettori nelle attività Test A/B, Automated Personalization e Test multivariato.

Per informazioni su quando utilizzare selettori CSS e quando utilizzare ID univoci, consulta Best practice e limitazioni del Compositore esperienza visivo.

Come Target genera un selettore per un elemento generate-selector

Target utilizza un algoritmo semplice per creare un selettore. La logica di generazione è la seguente:

  1. Se un elemento ha un ID, ad esempio id="container", il selettore per l’elemento è #container.

    Ad esempio:

    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>
    
  2. Se un elemento contiene un attributo di classe, Target tenta di utilizzare la prima classe di qualsiasi classe presente nell’elemento.

    Target tenta di analizzare l’elemento padre finché non trova l’elemento <HTML> o un elemento con un ID. Ogni volta che un elemento contiene un ID e il selettore viene calcolato sul relativo figlio discendente, l’ID di questo elemento contribuisce al selettore.

    Ad esempio:

    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>
    

    In questo esempio, il selettore è #container > ul.navigation:eq(0) > li.item:eq(0) (" > " indica l’elemento figlio diretto).

    eq fornisce l’indice dell’elemento che ha “tagName=UL” e la prima classe navigation. Pertanto, l’index è 0. Per ulteriori informazioni, leggi l’articolo sui selettori in MDN.

  3. Se un elemento non contiene una classe, Target utilizza tagName per l’elemento ed analizza l’elemento padre finché non viene trovato l’elemento <HTML> o un elemento con un ID.

    Ad esempio:

    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>
    

    Il selettore è #container > ul.navigation:eq(0) > li:nth-of-type(4).

In questo processo:

  • È possibile utilizzare qualsiasi selettore CSS, purché identifichi in modo univoco un elemento nel DOM.
  • L’approccio sopra riportato è quello utilizzato da Target. Target non rende obbligatorio questo approccio. È possibile aggiungere qualsiasi selettore, purché identifichi l’elemento in modo univoco.
  • Qualsiasi attributo può essere utilizzato nel selettore. Questo articolo utilizza solo un nome di classe come esempio.
AI Knowledge Reference

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 first li with the classes item active inside ul class="navigation" inside div id="container", the selector is #container > ul.navigation:eq(0) > li.item:eq(0) (" > " indicates the immediate child). eq gives the index of the element that has “tagName=UL” and the first class navigation, so index is 0.
  • Element without a class: Target uses tagName for the element and traverses up the parent element until either the <HTML> element or an element with an ID is found. For the fourth li (without a class) inside ul class="navigation" inside div 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)
recommendation-more-help
target-help-main