我对Javascript有点新鲜。我正在编写这个虚拟脚本来测试我的大页面需要什么。我想,最简单的是,获取href的值,然后更改href的值,然后再次查看它。为了简单起见,我现在已经删除了用于更改href值的部分,只是调用属性值两次。但是第二次访问属性值时,它显示了我在下面发布的错误。我发布了一个来自Firebug的错误日志和一个来自Dragonfly的错误日志(来自Opera)。
任何帮助都会非常感激。感谢。
我对脚本的另一个问题是它永远不会完成加载。我总是看到(在Firefox 3.6.8中)标签标题中的小加载标记。它不会打扰我,但如果有人有想法请告诉我。
<!-- This file is used to set attribute value for href. -->
<html>
<head>
<script type="text/javascript">
function hrefvalue()
{
var thisthang = document.getElementById("1").childNodes[0].getAttribute("href");
document.write(thisthang);
document.write("\n");
var newnew21 = document.getElementById("1").childNodes[0].getAttribute("href");
document.write(newnew21);
}
</script>
</head>
<body>
<div id="1"><a href="focusdude.htm">click here</a></div>
<button type="button" onclick="hrefvalue()">Click me instead</button>
</body>
</html>
错误日志:
1.萤火虫 - &gt; document.getElementById(“1”)为空
2.蜻蜓 - &gt;
未捕获的异常:TypeError:无法将'document.getElementById(“1”)'转换为对象
在第8行,第3列的hrefvalue()中抛出错误
file:// localhost / D:/Chicago%20pics/website%20pics/website%20root/trial5.htm:
var newnew21 = document.getElementById(“1”)。childNodes [0] .getAttribute(“href”);
从(事件)的第1行第0列调用:
hrefvalue()
再次感谢所有的鱼:D
答案 0 :(得分:3)
document.write
会废弃非打开的文档。当浏览器解析</html>
时,文档将关闭。由于函数运行onclick
,因此在您尝试编写之前它将被关闭。使用DOM methods使用JS修改HTML。