使用 window.location 时出现问题
我有一个 html 页面:
<html>
<head>
<script type="text/javascript">
function myfunct()
{
window.location='test.html';
document.getElementById("message")="hello";
}
</script>
</head>
<body>
<input type="button" value="click" onclick="myfunct()"/>
</body>
</html>
test.html:
<html><head></head>
<body>
<div id="message">Some text</div>
</body>
</html>
问题是 window.location
工作正常,但 div 没有更新为“hello”。
I have an html page:
<html>
<head>
<script type="text/javascript">
function myfunct()
{
window.location='test.html';
document.getElementById("message")="hello";
}
</script>
</head>
<body>
<input type="button" value="click" onclick="myfunct()"/>
</body>
</html>
test.html:
<html><head></head>
<body>
<div id="message">Some text</div>
</body>
</html>
The problem is that window.location
is working fine but the div is not getting updated to "hello".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法将字符串分配给元素,必须使用
innerHTML
属性。此外,由于该元素位于您打开的页面中,因此您必须在该页面中包含代码。test.html:
如果要从第一页发送消息,则必须将其作为数据发送到新页面,以便它可以显示它:
在第一页中:
在 test.html 中:
You can't assign a string to an element, you have to use the
innerHTML
property. Also, as the element is in the page that you open, you have to have the code in that page instead.test.html:
If you want to send the message along from the first page, you have to send it as data to the new page, so that it can display it:
In the first page:
In test.html:
你必须编写
函数 myfunct()
{
document.getElementById("消息")="你好";
在test.html
页面...
并在 test.html 的页面 Load 事件上调用此函数
you have to write
function myfunct()
{
document.getElementById("message")="hello";
}
in test.html page...
and call this function on page Load event of test.html
你必须写在 test.htm
而不是你的 html 中
you have to write in test.htm
not in your html
您尝试设置为“Hello World”的元素在您所在的页面上不存在。您只能访问当前页面上的元素。当页面刷新发生时,它会从请求的页面重新创建 HTML DOM。您无法直接访问其他页面的 DOM。
The element you are trying to set to 'Hello World' does not exist on the page you're function is on. You can only access elements on your current page. When a page refresh happens it recreates the HTML DOM from the requested page. You cannot access another pages DOM directly.