使用jquery

时间:2016-12-07 08:42:44

标签: jquery html5

您好我的页面中使用了一个标签,其中我有多个不同ID的跨度,如下所示:

<section class="blog-summary-read col-md-6 col-md-offset-1">
  <span id="spnid2">2</span>
  <span id="spnid3">3</span>
  <span id="spnid4">4</span>
  <span id="spnid5">5</span>
  <span id="spnid6">6</span>
</section>

如何使用jquery获取每个span id?

4 个答案:

答案 0 :(得分:5)

获取所有span元素,然后迭代以获取id。

std::vector
CArray

答案 1 :(得分:1)

&#42;
&#x002A;

使用$('span').each(function(i, v) { console.log($(this).attr('id')) })对每个范围进行迭代,然后使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section class="blog-summary-read col-md-6 col-md-offset-1"> <span id="spnid2">2</span> <span id="spnid3">3</span> <span id="spnid4">4</span> <span id="spnid5">5</span> <span id="spnid6">6</span> </section>

获取其ID

答案 2 :(得分:0)

现在您有3个版本可供选择...此版本仅选择“blog-summary-read”类中的部分内的跨度

var spans = $('span', '.blog-summary-read');
spans.each(function(index, element){
  console.log($(element).attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="blog-summary-read col-md-6 col-md-offset-1">
  <span id="spnid2">2</span>
  <span id="spnid3">3</span>
  <span id="spnid4">4</span>
  <span id="spnid5">5</span>
  <span id="spnid6">6</span>
</section>

答案 3 :(得分:0)

  

使用每个功能,您将获得所有跨度ID

&#13;
&#13;
$('.blog-summary-read span').each(function() {
  console.log(this.id);
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="blog-summary-read col-md-6 col-md-offset-1">
  <span id="spnid2">2</span>
  <span id="spnid3">3</span>
  <span id="spnid4">4</span>
  <span id="spnid5">5</span>
  <span id="spnid6">6</span>
</section>
&#13;
&#13;
&#13;