0

我对 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. Firebug -> document.getElementById("1") is null

2. Dragonfly ->
Uncaught exception: TypeError: Cannot convert 'document.getElementById("1")' to object


在file://localhost/D:/Chicago%20pics/website%20pics/website%20root/trial5.htm:
var newnew21 = document.getElementById("1")中 hrefvalue() 的第 8 行第 0 列抛出错误。 childNodes[0].getAttribute("href");
从(事件)中的第 1 行第 0 列调用:hrefvalue()

再次感谢所有的鱼:D

4

1 回答 1

3
  1. id 不能以数字开头(在 HTML 4 中)。不要押注支持此类 ID 的浏览器。
  2. document.write将丢弃未打开的文档。当浏览器解析</html>文档时关闭。由于该函数运行onclick,它将在您尝试编写之前关闭。使用DOM 方法通过 JS 修改 HTML。
于 2011-07-12T09:09:50.793 回答