最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to select the first div among other div siblings with JavascriptJquery - Stack Overflow

programmeradmin7浏览0评论

How do I select the div that contains "bar"?

<html>
<body>

<div id="foo">
    <div>bar</div> <!--this one-->
    <div>abc</div>
    <div>123</div>
</div>

</body>
</html>

How do I select the div that contains "bar"?

<html>
<body>

<div id="foo">
    <div>bar</div> <!--this one-->
    <div>abc</div>
    <div>123</div>
</div>

</body>
</html>
Share Improve this question edited Sep 7, 2018 at 12:37 Salman Arshad 272k84 gold badges443 silver badges534 bronze badges asked Dec 9, 2011 at 20:07 eastboundreastboundr 1,8778 gold badges28 silver badges46 bronze badges 1
  • Yep, thanks for the editing Dan. – eastboundr Commented Dec 9, 2011 at 20:29
Add a ment  | 

3 Answers 3

Reset to default 4

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)

This should do it:

$("#foo :first-child");

If you want based on contents rather than position, you could use this:

http://api.jquery./contains-selector/

$("div:contains('bar')")

发布评论

评论列表(0)

  1. 暂无评论