<html>
<head>
<style type="text/css">
#navigator a {
display: none;
}
</style>
<script type="text/javascript">
function view() {
var a = document.getElementsByTagName("a");
for (var i = 0; i < a.length; i++)
alert(a[i].innerHTML);
for (var i = 0; i < a.length; i++)
document.write(a[i].innerHTML);
}
</script>
</head>
<body>
<div id="navigator" class="navigator">
<div class="menu">
Programming Language
<a href="">C</a>
<a href="">C++</a>
<a href="">Java</a>
<button type="button" onclick="view()">View</button>
</div>
</div>
</body>
</html>
alert worked fine. document.write succeed with the first element ‘C’ but failed with the next two element.
Why did document.write fail with ‘no privilige’?
Thanks.
When you call
document.writeafter the page has loaded completely, you will replace the current page with what you write.When you write the HTML code of the first element, the element still exists, but when you try to write the HTML code of the second element, the element no longer exists.
If you really want to replace the current page with the html code of a few elements (which doesn’t really make sense as it’s not a complete HTML document), you have to gather the HTML code of all elements into a string, so that you can write it all at once: