使用jquery遍历DOM

时间:2016-03-17 15:08:14

标签: jquery dom traversal

如果我有以下div标签......

<!doctype html>
<div id="idea">
  <p class="news">hello world!</p>
  <p class="news">hello world!</p>
  <p class="news">hello world!</p>
  <p class="news">hello world!</p>
</div>

在jquery中,如果我想使用div id更改最后一个p标记并使用第二个p元素类来更改Hello world!别的东西。我的努力(没有工作):

  $(document).ready(function) {
    $('#idea:last').html("something else");
  }
  $(document).ready(function) {
    $('.newsbanner[1]').html("something else");
  }

2 个答案:

答案 0 :(得分:1)

您可以使用:nth-​​of-type()选择器。请检查此document

答案 1 :(得分:1)

npm run updateGitAndNpm

将更改第二个“.news”html

您尝试使用数组的示例不正确。您需要先声明数组。从css技巧中读取了n-child recepies,它们对于在JS中查找元素很有用。

https://css-tricks.com/useful-nth-child-recipies/

更新

$('.news:nth-child(2)').html("something else");

https://jsfiddle.net/516crdLd/

相关问题