[英]How to select the first div among other div siblings with Javascript/Jquery
How do I select the div that contains "bar"? 如何选择包含“ bar”的div?
<html>
<body>
<div id="foo">
<div>bar</div> <!--this one-->
<div>abc</div>
<div>123</div>
</div>
</body>
</html>
应该这样做:
$("#foo :first-child");
This is fastest as it does not use pseudo-selectors 这是最快的,因为它不使用伪选择器
$('#foo > div').first()
// same as
$('#foo > div').eq(0)
// same as
$('#foo > div').slice(0,1)
If you want based on contents rather than position, you could use this: 如果要基于内容而不是位置,则可以使用以下方法:
http://api.jquery.com/contains-selector/ http://api.jquery.com/contains-selector/
$("div:contains('bar')")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.