document.body.querySelector和document.querySelector有什么区别?
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h2 class="example">A heading with class="example"</h2>
<p class="example">A paragraph with class="example".</p>
<p>Click the button to add a background color to the first element in the document with class="example".</p>
<button onclick="myFunction()">Submit</button>
<script>
function myFunction() {
//document.body.querySelector('title').text = "title changed"; //Uncaught TypeError: Cannot set property 'text' of null
document.querySelector('title').text = "title changed"; // work fine
// document.body trageting only those elements which are inside body tag.
}
</script>
</body>
</html>