html JS.DOM.AccessHtml.QuerySelectorAll.Ex1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html JS.DOM.AccessHtml.QuerySelectorAll.Ex1相关的知识,希望对你有一定的参考价值。

function changeBackGroundOfPs(id) {
  var paragraphs = document.querySelectorAll("#" + id + " p");

  // Another way to iterate on all elements in a collection
  for (var i = 0; i < paragraphs.length; i++ ) {
    paragraphs[i].style.backgroundColor = "lightGreen";
  }
}
JS.DOM.AccessHtml.QuerySelectorAll.Ex1
--------------------------------------


A [Pen](https://codepen.io/Onlyforbopi/pen/vzwYLE) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).

[License](https://codepen.io/Onlyforbopi/pen/vzwYLE/license).
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Change background color of p under an element whose id is known</title>
</head>
<body>
  <button onclick="changeBackGroundOfPs('firstDiv');">Change backgrounds of p under a given element known by id</button>
  <br>
<div id="firstDiv">
  <p>First paragraph.</p>
  <p>Second paragraph.</p>
</div>
</body>
</html>

以上是关于html JS.DOM.AccessHtml.QuerySelectorAll.Ex1的主要内容,如果未能解决你的问题,请参考以下文章

html Html模板/ Html Boilerplate |标签HTML

html里怎么引用一个html的头部

html5与传统html区别

html4和html5的区别

HTML元素

head标签怎么给多个html引用