DOM nav对象用于表示HTML <nav>元素。 <nav>元素由getElementById()访问。
用法:
document.getElementById("id")
id分配给<nav>标签的位置。
注意:Internet Explorer 8和更早版本不支持Nav对象。
范例1:
<!DOCTYPE html>
<html>
<head>
<title>
HTML DOM Nav Object
</title>
</head>
<body>
<h1>
GeeksforGeeks
</h1>
<h2>HTML DOM nav Object</h2>
<nav id = "nav_object">
<a href="#">Data Structure</a> |
<a href="#">Algorithm</a> |
<a href="#">Programming Languages</a>
</nav>
<br>
<button onclick = "Geeks()">
Click Here!
</button>
<p id = "sudo"></p>
<script>
function Geeks() {
var obj = document.getElementById("nav_object").innerHTML;
document.getElementById("sudo").innerHTML = obj;
}
</script>
</body>
</html>
输出:
在单击按钮之前:
单击按钮后:
范例2:可以使用document.createElement方法创建导航对象。
<!DOCTYPE html>
<html>
<head>
<title>
HTML DOM nav Object
</title>
</head>
<body>
<h1 style = "color:green;" >
GeeksForGeeks
</h1>
<h2>DOM nav Object</h2>
<button onclick = "Geeks()">
Click Here!
</button>
<br><br>
<script>
function Geeks() {
var ele = document.createElement("NAV");
document.body.appendChild(ele);
var a = document.createElement("A");
a.setAttribute("href", "/html");
var txt = document.createTextNode("Home");
a.appendChild(txt);
ele.appendChild(a);
}
</script>
</body>
</html>
输出:
在单击按钮之前:
单击按钮后:
相关用法
- HTML <nav>用法及代码示例
- HTML DOM Object用法及代码示例
- CSS nav-left用法及代码示例
- CSS nav-up用法及代码示例
- CSS nav-down用法及代码示例
- CSS nav-right用法及代码示例
- CSS nav-index用法及代码示例
- HTML DOM HTML用法及代码示例
- HTML DOM Input Week用法及代码示例
- HTML DOM Column用法及代码示例
- HTML DOM Del用法及代码示例
- HTML DOM Embed用法及代码示例
- HTML DOM Header用法及代码示例
- HTML DOM Footer用法及代码示例
- HTML DOM Span用法及代码示例
- HTML DOM HR用法及代码示例
注:本文由纯净天空筛选整理自Vishal Chaudhary 2大神的英文原创作品 HTML | DOM Nav Object。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。