0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

javascriptの勉強記録

Last updated at Posted at 2022-12-25

li要素のテキスト取得

<ul id="list">
  <li>aaa</li>
  <li>bbb</li>
  <li>ccc</li>
</ul>

<script>
  var elem = document.getElementById('list');
  console.log(elem.children.item(1).textContent);
  //bbb
</script>

CSSの疑似クラス:nth-child()とquerySelector()で取得する方法
(コメントで教えて頂きました)

<ul id="list">
  <li>aaa</li>
  <li>bbb</li>
  <li>ccc</li>
</ul>

<script>
console.log(document.querySelector('#list li:nth-child(2)').textContent);
// bbb

console.log(list.querySelector('li:nth-child(2)').textContent);
// bbb
</script>
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?