HTML DOM Ol 对象
JavaScript基础 2022-05-13 16:21:5314小码哥的IT人生shichen
HTML DOM Ol 对象
Ol 对象
Ol 对象表示 HTML <ol>
元素。
访问 Ol 对象
您可以通过使用 getElementById()
来访问 <ol>
元素:
var x = document.getElementById("myOl");
完整实例:
<!DOCTYPE html> <html> <body> <h3>演示如何访问 OL 元素</h3> <ol id="myOl"> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol> <p>点击按钮来设置列表项的数字从 25 开始递增。</p> <button onclick="myFunction()">试一下</button> <script> function myFunction() { var x = document.getElementById("myOl"); x.start = "25"; } </script> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
创建 Ol 对象
您可以通过使用 document.createElement()
方法来创建 <ol>
元素:
var x = document.createElement("OL");
完整实例:
<!DOCTYPE html> <html> <body> <h3>演示如何创建 OL 元素</h3> <p>点击按钮来创建 OL 元素和 LI 元素。</p> <p id="demo"></p> <button onclick="myFunction()">试一下</button> <script> function myFunction() { var x = document.createElement("OL"); x.setAttribute("id", "myOl"); document.body.appendChild(x); var y = document.createElement("LI"); var t = document.createTextNode("咖啡"); y.appendChild(t); document.getElementById("myOl").appendChild(y); } </script> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
Ol 对象属性
属性 | 描述 |
---|---|
compact |
HTML5 中不支持。请使用 style.lineHeight 代替。 设置或返回列表是否应该比正常值显示得略小。 |
reversed | 设置或返回列表顺序是否为降序。 |
start | 设置或返回有序列表的 start 属性值。 |
type | 设置或返回有序列表的 type 属性值。 |
标准属性和事件
相关页面
HTML 参考手册:HTML <ol> 标签