简体   繁体   English

显示来自多个具有相同类名的 DIV 的第一个 div

[英]Show first div from multiple DIVs with same class name

I'm looping through a resultset which produces the following HTML:我正在循环生成以下 HTML 的结果集:

<div class="main">
<div class="total-runs innerdiv showonce">1</div>
<div class="total-runs showonce">2</div>
</div>
<div class="main">
<div class="total-runs innerdiv showonce">3</div>
<div class="total-runs showonce">4</div>
</div>

i want to show only first div class name is Showonce我只想显示第一个 div 类名是 Showonce

Result need to be like结果需要像

1
3

You can use eq(index) for this:您可以为此使用eq(index)

 $('.showonce').hide().eq(0).show();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="total-runs innerdiv showonce">1</div> <div class="total-runs showonce">2</div> <div class="innerdiv showonce">3</div>

EDIT:编辑:

 $('.main .showonce').hide() $('.main .showonce:first-child').show()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="main"> <div class="total-runs innerdiv showonce">1</div> <div class="total-runs showonce">2</div> </div> <div class="main"> <div class="total-runs innerdiv showonce">3</div> <div class="total-runs showonce">4</div> </div>

There are multiple ways to do it有多种方法可以做到

the .first() method constructs a new jQuery object from the first element in that set(source : https://api.jquery.com/first/ .first()方法从该集合中的第一个元素构造一个新的 jQuery 对象(来源: https : //api.jquery.com/first/

<script>
     $(document).ready(function(){     
      $(".main .showonce:first-child").css("background-color", "yellow");
     });
 </script>

在此处输入图片说明

在此处输入图片说明

$('div').first()

$(".showonce:first")

OR或者

var divValue = $('div:first')

OR或者

$('div').eq(0)

$(".showonce").eq(0)

i have read you question.我读过你的问题。

it is very easy to show only first and third child of your div you can simply add the following CSS in your style tag it will be done很容易只显示 div 的第一个和第三个孩子,您只需在样式标签中添加以下 CSS 即可完成

<!DOCTYPE html>
<html>
<head>


<style>
.showonce{
display:none;
}

.innerdiv{
display:block;
}
</style>
</head>
<body>


<p>The last paragraph in body.</p>

<div class="main">
<div class="total-runs innerdiv showonce">1</div>
<div class="total-runs showonce">2</div>
</div>
<div class="main">
<div class="total-runs innerdiv showonce">3</div>
<div class="total-runs showonce">4</div>
</div>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示/隐藏具有相同类别名称的多个DIV - Show/Hide multiple DIVs with same class name 隐藏当前的div,并在同一班级的多个div中显示另一个 - Hide current div and show another among multiple divs with same class 显示和隐藏具有相同类名的多个div不起作用 - Show and hide multiple divs with same class name not working 显示/隐藏具有相同类名的div - Show / Hide divs individually with same class name 如果有多个具有相同类名的父div,则子div类没有内容时,隐藏父div类 - Hide parent div class if child div class does not have content when there are multiple parent divs with the same class name 找到第一个类,然后逐渐将类添加到具有相同名称的所有div - Find first class then gradually add class to all divs with same name 获取被单击元素的ID名称,并显示具有相同类名称的div - Get ID name of a clicked element and show divs with the same name of class javascript函数显示/隐藏多个div,第一个div可见 - javascript function show / hide multiple divs with first div visible 如何将Ajax调用返回的数据附加到动态创建的div(具有相同类名的许多div中) - how to append data returned from ajax call to a dynamically created div, among many divs of same class name 附加到最近的div类(页面上具有相同类的多个div) - Append to closest div class (multiple divs with same class on page)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM