JavaScript 获取 #document 数据完全指南

在 JavaScript 中,'document' 对象是操作网页的钥匙。'#document' 则代表整个 HTML 文档的根节点,可以通过 'document.documentElement' 访问。

本文将带你学习如何使用 JavaScript 获取 #document 的各种数据,并提供代码示例。

1. 获取整个 HTML 文档内容javascriptconst htmlContent = document.documentElement.innerHTML;console.log(htmlContent); // 输出整个 HTML 文档内容

2. 获取网页标题javascriptconst pageTitle = document.title;console.log(pageTitle); // 输出 标签内的文本内容</h3> <h3>3. 获取当前页面 URLjavascriptconst currentURL = document.URL;console.log(currentURL); // 输出当前页面的 URL 地址</h3> <h3>4. 获取页面域名javascriptconst domainName = document.domain;console.log(domainName); // 输出当前页面的域名</h3> <h3>5. 获取字符集编码javascriptconst charset = document.characterSet;console.log(charset); // 输出当前页面使用的字符集编码</h3> <h3>6. 获取 <head> 标签内容javascriptconst headContent = document.head;console.log(headContent); // 输出 <head> 标签内的所有内容</h3> <h3>7. 获取 <body> 标签内容javascriptconst bodyContent = document.body;console.log(bodyContent); // 输出 <body> 标签内的所有内容</h3> <h3>8. 获取所有 HTML 元素javascriptconst allElements = document.querySelectorAll('*'); console.log(allElements); // 输出一个包含文档中所有元素的 NodeList 集合</h3> <p><strong>注意:</strong> 'document.all' 虽然也能获取所有元素,但已不被推荐使用。建议使用 'document.querySelectorAll('*')' 代替。</p> <p>希望这些方法能帮助你更好地理解和使用 JavaScript 操作 #document 数据!</p> <img src="http://copyright.bdstatic.com/vcg/creative/8aed1e8b8dd55568a54d6fd9a91d6093.jpg" alt="JavaScript 获取 #document 数据完全指南" title="JavaScript 获取 #document 数据完全指南" loading="lazy"> </div> <br/> <p>原文地址: <a href="https://www.cveoy.top/t/topic/fTMC">https://www.cveoy.top/t/topic/fTMC</a> 著作权归作者所有。请勿转载和采集!</p> <p><a href="http://ai66.chagpt.fun/" target="_blank" style="color: red;">免费AI点我,无需注册和登录</a></p> </article> <ul class="post-near"> <li> 上一篇: <a href="/t/topic/fTMD" title="Flow sensors or meters measure volumetric flow rates They also rely on previously presented technologyThe most common ones use either a measurement of a speed of a mechanical part turbine propeller ge">Flow sensors or meters measure volumetric flow rates They also rely on previously presented technologyThe most common ones use either a measurement of a speed of a mechanical part turbine propeller ge</a> </li> <li> 下一篇: <a href="/t/topic/fTMB" title="Uncaught DOMException: Failed to execute 'appendChild' - How to Fix">Uncaught DOMException: Failed to execute 'appendChild' - How to Fix</a> </li> </ul> </div><!-- end #main--> </div> </div> </div> <footer id="footer" role="contentinfo"> <p>© 2019 • 2025 - 安全问答 站长邮箱:wxgpt@qq.com    ICP备案/许可证号:<a href="https://beian.miit.gov.cn/" target="_blank">豫ICP备2024104334号-2</a></p> </footer> </body> <script> (function() { // 获取当前域名(不含端口) const hostname = window.location.hostname; // 只在指定域名加载广告 const allowHosts = ["www.cveoy.top"]; if (allowHosts.includes(hostname)) { // 动态插入 Google Ads 脚本 const script = document.createElement("script"); script.async = true; script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9203274547589271"; script.crossOrigin = "anonymous"; document.head.appendChild(script); } })(); </script> <script async type="text/javascript"> (function(c,l,a,r,i,t,y){ c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); })(window, document, "clarity", "script", "oxccvyk7pu"); </script> </html>