dom(文档对象模型)是web文档的编程接口,它将html或xml文档的结构表示为对象树。借助dom,开发者可以使用javascript与网页交互并操作网页内容。html dom允许javascript更改html元素的内容、属性和样式。

在HTML页面中查找和访问HTML元素的方法:
<code class="javascript">const element = document.getElementById("intro");</code><p>标签:<code class="javascript">const elements = document.getElementsByTagName("p");</code><code class="javascript">const elements = document.getElementsByClassName("intro");</code>querySelectorAll()方法可以查找匹配指定CSS选择器的所有HTML元素。例如,查找类名为"intro"的<p>标签:<code class="javascript">const elements = document.querySelectorAll("p.intro");</code>DOM中的所有内容都是节点,包括元素、文本和属性。
<div>、<p>、<h1>等。 例如:<code class="html"><h1>Hello, world!</h1></code>
创建新的HTML元素(节点):
立即学习“Java免费学习笔记(深入)”;
document.createElement()方法创建HTML元素。document.createTextNode()方法创建文本节点。appendChild()方法将文本节点添加到新创建的元素。appendChild()方法将新创建的元素添加到文档中的某个父元素(例如,<body>)。示例:
<code class="html"><div id="para"></div>
<script>
const elem = document.createElement("h1");
const text = document.createTextNode("Payilagam");
elem.appendChild(text);
document.getElementById("para").appendChild(elem);
</script></code>更改HTML样式:
语法:document.getElementById(id).style.property = newStyle;
示例:
<code class="html"><div id="para" onclick="change()"><h1>Payilagam</h1></div>
<script>
function change() {
para.style.color = "red";
}
</script></code>
(图片描述:点击前后的样式变化)
以上就是JavaScript-文档对象模型(DOM)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号