2017-06-15 107 views
0

我是新來的JS,我想動態的基礎上刪除某些條件上頁腳中的環節之一。 但隨着嵌套在多類我頁腳中的代碼刪除的鏈接是:有沒有更好的方法來編寫後代選擇器?

$(".footer .footer__container .footer__information .footer__links .footer__link").last().empty(); 

有沒有更好的方式來寫這一行有多個類嵌套? 只是一個註釋:在我原來的項目文件中,「.footer」類是暴露給我的文件。所以我的頂級課程必須以「.footer」開頭。

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Delete link in a footer demo</title> 
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script> 
</head> 
<body> 

<p> 
    Hello, <span>Person</span> <em>and person</em>. 
</p> 

<!-- footer has 3 links of which last link needs to be deleted on button press --> 
<footer role="footer" class="footer"> 
    <div class="footer__container"> 
     <div class="footer__logo"> 
      <img src="images.png" alt="Images"> 
     </div> 
     <div class="footer__information"> 
      <div class="footer__links"> 
       <a target="_blank" href="https://link1" aria-label="link1" class="footer__link">link1</a> 
       <a target="_blank" href="https://link2" aria-label="link2" class="footer__link">link2</a> 
       <a target="_blank" href="https://link3" aria-label="link3" class="footer__link">link3</a> 
      </div> 
     </div> 
    </div> 
</footer> 
<button>Call empty() on above paragraph</button> 

<script> 
    $("button").click(function() { 
     $(".footer .footer__container .footer__information .footer__links .footer__link").last().remove(); 
    }); 
</script> 

</body> 
</html> 
+2

你有多少內容不同的頁腳?沒有理由成爲特定的。只要做'$(「。footer__link」)'。 – Bergi

+0

「*在我的原始項目文件中,」.footer「類是暴露給我的文件,所以我的頂級類必須以」.footer「*」「開始 - 呃,什麼?我不這麼認爲。順便說一句,當標籤選擇器滿足時,你根本不需要任何類。 '$(「footer a」)'可以。 – Bergi

+0

或'a.footer_link',或... – RobG

回答

0

除非您有另一部分HTML具有類似的內部類,否則不需要那麼具體。所有你需要在這種情況下是

$(".footer_link").last().remove(); // or .empty(), whichever you intended 
相關問題