簡體   English   中英

Javascript或jQuery選擇器從`a`然后到很多div使用類

[英]Javascript or jQuery selector from `a` then to much div using class

我有像這樣的HTML

<a href="/portal/product/62157993" id="ember1708" class="product-items__item ember-view"> 
  <div id="ember1709" class="product-item product-view-item ember-view">
    <div class="product-item__info">
      <div class="product-item__name">
        <span data-ember-action="" data-ember-action-1712="1712">Pashmina Denim</span>
      </div>
      <div class="product-item__promote">
        <!---->
        <div id="ember1713" class="product-boost-button ember-view">
          <div id="ember1714" class="shopee-button shopee-button--inactive shopee-button--medium ember-view">  Promote
          </div>
        </div>
      </div>
    </div>
  </div>
</a>

現在,我想使用僅使用CLASS a href="/portal/product/62157993"選擇器單擊帶有TEXT“Promote”的DIV,而不是使用ID

我已嘗試使用以下代碼,但在我的情況下不起作用

var aTags = document.querySelectorAll("a[href='/portal/product/62157993'],div[class='product-item'],div[class='product-item__info'],div[class='product-item__promote'],div[class='product-boost-button]',div[class='product-boost-button]',div[class='shopee-button]'");

我該如何解決這個問題?

如果您想選擇<a href="/portal/product/62157993" id="ember1708" class="product-items__item ember-view">特定子元素,您可以這樣做。

 var promote = document.querySelector('.product-items__item .product-item__promote'); promote.addEventListener('click', function(e){ e.preventDefault(); console.log('prmotion ending'); }); 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <a href="/portal/product/62157993" id="ember1708" class="product-items__item ember-view"> <div id="ember1709" class="product-item product-view-item ember-view"> <div class="product-item__info"> <div class="product-item__name"> <span data-ember-action="" data-ember-action-1712="1712">Pashmina Denim</span> </div> <div class="product-item__promote"> <div id="ember1713" class="product-boost-button ember-view"> <div id="ember1714" class="shopee-button shopee-button--inactive shopee-button--medium ember-view">Promote </div> </div> </div> </div> </div> </a> </body> 

這樣可以確保您只選擇了.product-items__item類的子項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM