开发者社区> 问答> 正文

jquery中first-child,在这种情况下为什么会无效呢?-jQuery报错

"

为什么多了那个h2标签,jQuery中的first-child就无法生效呢?见下图所示。谢谢了

<pre class=""brush:html; toolbar: true; auto-links: false;""><html><head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('div p:first-child').css('backgroundColor', '#555'); }); </script> </head><body><html> <div> <h2>hello</h2> <p>A</p> <p>B</p> <p>C</p> </div> <div> <p>D</p> <p>E</p> <p>F</p> </div> <div> <p>G</p> <p>H</p> <p>I</p> </div> </body></html>




"

展开
收起
montos 2020-05-31 16:22:14 370 0
1 条回答
写回答
取消 提交回答
  • "first-child只会遍历到第一个子元素######因第一个div里,没有第一个且p的元素######$("div > p").first()######但是选择的是p标签,并不是h2啊?######<div class=""ref"">

    引用来自“con”的评论

    $("div > p").first() 不行。只有A有效果,D和G没有效果。######哦哦,没错,用first是不行的 $("div > p(:not(p + p))")######nth-of-type"
    2020-05-31 16:22:24
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Javascript中的函数 立即下载
JavaScript函数 立即下载
Javascript中的对象 立即下载