Javascript jQuery将样式删除或添加到特定类型元素的最后一个

Javascript jQuery将样式删除或添加到特定类型元素的最后一个,javascript,jquery,for-loop,element,Javascript,Jquery,For Loop,Element,我有一个jQuery页面,其中有一些内容。所有功能都已完成。唯一不完整的是删除每个表的最后一个hr(仅最后一个)。我需要知道如何找到数组中特定类型元素的最后一个。表中包装的所有元素都是它们自己的数组。我需要遍历每个表,只删除每个表中的最后一个hr。我试过很多方法,但似乎都没有成功。下面的代码只删除最后一个表的最后一个hr。这不能完成我的任务 jQuery/JavaScript代码(除了删除最后一个HR之外,其他一切都可以正常工作) $(文档).ready(函数(){ var H2SingerT

我有一个jQuery页面,其中有一些内容。所有功能都已完成。唯一不完整的是删除每个表的最后一个hr(仅最后一个)。我需要知道如何找到数组中特定类型元素的最后一个。表中包装的所有元素都是它们自己的数组。我需要遍历每个表,只删除每个表中的最后一个hr。我试过很多方法,但似乎都没有成功。下面的代码只删除最后一个表的最后一个hr。这不能完成我的任务

jQuery/JavaScript代码(除了删除最后一个HR之外,其他一切都可以正常工作)


$(文档).ready(函数(){
var H2SingerTitles=$(“h2.toggleSinger”);
对于(i=2;i
内部循环(如下所示)是我试图使用它逐个循环表,然后只删除表中的最后一个HR。由于某种原因,它不起作用

for (j = 0; j < siblingsofH2Singer.length; j++) {
  $("hr").last().css("border-top", "none");
} // line ends for loop with j counter
(j=0;j{ $(“hr”).last().css(“边框顶部”、“无”); }//带j计数器的循环的行结束 下面是我的HTML

<div id="mainPage">
  <h2 class=""><span class="title">Whitney Houston</span></h2>
  <div class="aboutInfo">
    <img src="whitney_houston.jpg" height="150">
    <a href="www.whitneyhouston.com" target="_blank"><h3><em>The Voice</em> 
    </h3></a>
    <h3>Worldwide Famous Vocalist, Singer and Performer</h3> 
    <p>I will always love you</p>
    <br>
    <hr>
  </div> 

  <h2 class=""><span class="title">Mariah Carey</span></h2>
  <div class="aboutInfo">
    <img src="mariah_carey.jpg" height="150">
    <a href="www.mariahcarey.com" target="_blank"><h3><em>Whistle 
    Register</em></h3></a>
    <h3>Worldwide Famous Vocalist, Singer and Performer</h3> 
    <p>Hero</p>
    <br>
    <hr>
  </div>

  <h2 class="toggleSinger"><span class="title">Michael Jackson</span></h2>
  <table style="display: none;">
    <div class="aboutInfo">
      <img src="michael_jackson.jpg" height="150">
      <a href="www.michaeljackson.com" target="_blank"><h3><em>King of
      Pop</em></h3></a>
      <h3>Worldwide Superstar Singer, Dancer and Performer</h3> 
      <p>Billie Jean</p>
      <br>
      <hr>
    </div>
    <div class="aboutInfo">
      <img src="jackson_5.jpg" height="150">
      <a href="www.jackson5.com" target="_blank"><h3><em>The Jackson 5</em> 
      </h3></a>
      <h3>Worldwide Famous R and B Group</h3> 
      <p>ABC</p>
      <br>
      <hr>
    </div>
  </table>

  <h2 class="toggleSinger"><span class="title">Diana Ross</span></h2>
  <table style="display: none;">
    <div class="aboutInfo">
      <img src="diana_ross.jpg" height="150">
      <a href="www.dianaross.com" target="_blank"><h3><em>The Original 
      Diva</em></h3></a>
      <h3>Worldwide Superstar Singer</h3> 
      <p>I'm coming out</p>
      <br>
      <hr>
    </div>
    <div class="aboutInfo">
      <img src="diana_ross.jpg" height="150">
      <a href="www.dianaross.com" target="_blank"><h3><em>The Supremes</em> 
      </h3></a>
      <h3>Worldwide Famous Girl Group</h3> 
      <p>Stop in the name of love</p>
      <br>
      <hr>
    </div>
  </table>

  <h2 class="toggleSinger"><span class="title">Lionel Richie</span></h2>
  <table style="display: none;">
    <div class="aboutInfo">
      <img src="lionel_richie.jpg" height="150">
      <a href="www.lionelrichie.com" target="_blank"><h3><em></em></h3></a>
      <h3>Worldwide Superstar Singer and Pianist</h3> 
      <p>All night long</p>
      <br>
      <hr>
    </div>
    <div class="aboutInfo">
      <img src="diana_ross.jpg" height="150">
      <a href="www.dianaross.com" target="_blank"><h3><em>The Commodores</em> 
      </h3></a>
      <h3>Worldwide Famous Group and Band</h3> 
      <p>Brickhouse</p>
      <br>
      <hr>
    </div>
  </table>

  <h2 class="toggleSinger"><span class="title">Smokey Robinson</span></h2>
  <table style="display: none;">
    <div class="aboutInfo">
      <img src="smokey_robinson.jpg" height="150">
      <a href="www.smokeyrobinson.com" target="_blank"><h3><em></em></h3></a>
      <h3>Worldwide Singer and Songwriter</h3> 
      <p>Cruising</p>
      <br>
      <hr>
    </div>
    <div class="aboutInfo">
      <img src="the_miracles.jpg" height="150">
      <a href="www.themiracles.com" target="_blank"><h3><em>The Miracles</em> 
      </h3></a>
      <h3>Worldwide Famous R and B Group</h3> 
      <p>Ooh Baby Baby</p>
      <br>
      <hr>
    </div>
  </table>
</div>

惠特尼休斯顿
世界著名歌唱家、歌手和表演者
我将永远爱你



玛丽亚·凯莉 世界著名歌唱家、歌手和表演者 英雄



迈克尔杰克逊 全球超级明星歌手、舞蹈家和表演者 比莉·琼



世界著名的R和B集团 ABC



戴安娜·罗斯 世界巨星歌手 我要出来了



世界著名女子团体 以爱的名义停止



莱昂纳尔·里奇 世界超级明星歌手和钢琴家 通宵



世界著名乐队 砖房



斯莫基·罗宾逊 全球歌手和歌曲作者 巡航



世界著名的R和B集团 哦,宝贝




如果要删除每个表上不需要for循环的最后一个HR,请尝试此操作

$("table").each(function(){ $(this).find("hr").last().remove(); });

$('hr:last child')。删除()或,使用CSS:
hr:last child{display:none;}
?谢谢,但当我输入此代码时,它会弄乱页面上的一些其他元素,并导致一些图像发生移动。你能提供一个指向活动页面的链接吗?提供的代码确实删除了每个表中的最后一个HR,所以可能您的要求有点不同?很好,我能够让它工作。非常感谢你。
$("table").each(function(){ $(this).find("hr").last().remove(); });