document.getElementById 抛出 TypeError

发布于 2024-11-19 12:00:38 字数 1415 浏览 6 评论 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. 萤火虫-> document.getElementById("1") 为 null

2. 蜻蜓->
未捕获的异常:TypeError:无法将 'document.getElementById("1")' 转换为对象


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

再次感谢所有的鱼:D

I am a bit new to Javascript. I am writing this dummy script to test out what I need for my larger page. I want, at the simplest, to get the value of href and then change the value of href and then see it again. TO keep it simple, right now I have removed the part for changing the href value and just calling the attribute value twice. But the second time I access the attribute value it show an error which I have posted below. I'm posting two error logs one from Firebug and one from Dragonfly(from Opera).

Any help would be dearly appreciated. Thanks.

One more issue I have with the script is that it never finishes loading. I always see (in Firefox 3.6.8) the little loading sign going round and round in the tab title. It doesn't bother me so much but if anyone has an idea please tell me.

<!-- 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>

Error logs :
1. Firebug -> document.getElementById("1") is null

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

Error thrown at line 8, column 0 in hrefvalue() in
file://localhost/D:/Chicago%20pics/website%20pics/website%20root/trial5.htm:
var newnew21 = document.getElementById("1").childNodes[0].getAttribute("href");
called from line 1, column 0 in (event):
hrefvalue()

And thanks again for all the fish :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2024-11-26 12:00:38
  1. id 不能以数字开头(在 HTML 4 中)。不要押注于支持此类 ID 的浏览器。
  2. document.write 将丢弃未打开的文档。当浏览器解析 时,文档就会关闭。由于该函数运行onclick,因此它会在您尝试写入之前关闭。使用DOM方法用JS修改HTML。
  1. An id can't start with a number (in HTML 4). Don't bet on browsers supporting such ids.
  2. document.write will trash a document that isn't open. When the browser parses </html> the document is closed. Since the function runs onclick, it will be closed before you try to write. Use DOM methods to modify HTML with JS.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文