HTML DOM Paragraph 对象
JavaScript基础 2022-05-13 16:22:05小码哥的IT人生shichen
HTML DOM Paragraph 对象
Paragraph 对象
Paragraph 对象代表 HTML <p> 元素。
访问 Paragraph 对象
您可使用 getElementById() 来访问 <p> 元素:
示例代码:
var x = document.getElementById("myP");
完整实例:
<!DOCTYPE html>
<html>
<body>
<h3>如何访问 P 元素的演示</h3>
<p id="myP">我是一个段落。点击按钮来获取我的 ID。</p>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myP").id;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
创建 Paragraph 对象
您可使用 document.createElement() 方法来创建 <p> 元素:
示例代码:
var x = document.createElement("P");
完整实例:
<!DOCTYPE html>
<html>
<body>
<p>单击该按钮以创建 P 元素。</p>
<button onclick="myFunction()">试一试</button>
<script>
function myFunction() {
var x = document.createElement("P");
var t = document.createTextNode("This is a paragraph.");
x.appendChild(t);
document.body.appendChild(x);
}
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
Paragraph 对象属性
| 属性 | 描述 |
|---|---|
| align |
HTML5 中不支持。请改用 style.textAlign。 设置或返回段落的 align 属性值。 |

