繁体   English   中英

HTML / Javascript Document.getelementbyid无法正常工作

[英]Html/Javascript Document.getelementbyid Not working

我是中级HTML,并且开始学习其中的JavaScript。

 <html> <head> <title>Uh oh! No page.</title> <h1 class="index">The page you requested for is not available! Click <a href="index.html" class="index">here</a> to go back</h1> <style> .index { text-decoration: none; font-family: Arial; } </style> </head> <body onload="myfunc()"> <script> function myfunc() { var x; if (confirm("Im confuzzled! Where did the page go?\\n\\nSelect OK to be redirected and Cancel to do it manually ") == true) { window.location = "index.html"; } else { alert("You will now manually have to redirect to the lemoon homepage"); } document.getElementById("demo").innerHTML = x; } </script> </body> <footer onload="pageLocate()"> <script> function pageLocate() { document.getElementById("demo").innerHTML = "Page location is: " + window.location.href; } </script> </footer> </html> 
我无法使文档标签显示任何内容。 你能帮忙吗?

我已经扫描了两次您的代码,但仍然看不到id =“ demo”的标签在哪里。 如果您没有创建一个带有ID的标记(在本例中为“ demo”),那么当您调用它时,如果找不到目标,该如何进行控制呢? 我相信这是你的错

没有ID为“ demo”的元素。 您需要提供要执行id为“ demo”的功能的元素,此功能才能起作用。

例如:

<div id="demo">Hello!</div>

您不必问这个问题,只需要花一点点思考或在Google上进行一些搜索即可。

我真的希望您想要这样的东西,因为那里有些不太合逻辑的东西。

1.footer和h1标签属于页面正文(内部标签)。 2.您几乎创建了一个函数来获取另一个函数的一些数据,这些数据可以从另一个函数访问

 <html> <head> <title>Uh oh! No page.</title> <style> .index { text-decoration: none; font-family: Arial; } </style> <script> function myfunc() { if (confirm("Im confuzzled! Where did the page go?\\n\\nSelect OK to be redirected and Cancel to do it manually ") == true) { window.location = "index.html"; } else { alert("You will now manually have to redirect to the lemoon homepage"); } document.getElementById("demo").innerHTML = "Page location is: " + window.location.href; } </script> </head> <body onload="myfunc()"> <h1 class="index">The page you requested for is not available! Click <a href="index.html" class="index">here</a> to go back</h1> <footer id="demo" onload="pageLocate()"> </footer> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM