How to hide div that without id/class by jquery?

不问归期 提交于 2019-12-22 23:26:29

问题


How to hide first/second div with jQuery?

Div can not get id/class!

<body>
<div>
<p>ssssssss</p>
</div>
<div>
<p>ttttttttt></p>
</div>
<div>
<p>fffff</p>
</div>
</body>

回答1:


To hide the first <div> element, do:

$("div:eq(0)").hide();

To hide the second <div> element, do:

$("div:eq(1)").hide();

To hide both the first and the second <div> elements, do:

$("div:lt(2)").hide();



回答2:


$('div:lt(2)').hide();



回答3:


$("body div:nth-child(1)").hide(); perhaps? untested

HTML markup is needed to help further

edit - ah ha, code block fail - my code still stands but i suspect there's a better way




回答4:


You can use pseudo-selectors :first and :nth(n)... Note that nth(n) is zero-based. So :nth(0) is equivalent to :first.

$('div:first').hide();
$('div:nth(1)').hide();



回答5:


to hide child div 1 and child div 2 (or 3 or 4)

   $('body div:nth-child(1),body div:nth-child(2)').hide();

nth-child is the offset and the comma seperates out which ones.. in case you wanted to keep div# 2 it could be

 $('body div:nth-child(1),body div:nth-child(3)').hide();


来源:https://stackoverflow.com/questions/4319279/how-to-hide-div-that-without-id-class-by-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!