使用 window.location 时出现问题

发布于 2024-10-31 12:27:13 字数 577 浏览 7 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(4

祁梦 2024-11-07 12:27:13

您无法将字符串分配给元素,必须使用 innerHTML 属性。此外,由于该元素位于您打开的页面中,因此您必须在该页面中包含代码。

test.html:

<html>
<head>
<script type="text/javascript">

window.onload = function() {
  document.getElementById("message").innerHTML = "hello";
}

</script>

</head>
<body>
<div id="message">Some text</div>
</body>
</html>

如果要从第一页发送消息,则必须将其作为数据发送到新页面,以便它可以显示它:

在第一页中:

window.location.href = 'test.html?hello';

在 test.html 中:

window.onload = function() {
  document.getElementById("message").innerHTML = window.location.search.substr(1);
}

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:

<html>
<head>
<script type="text/javascript">

window.onload = function() {
  document.getElementById("message").innerHTML = "hello";
}

</script>

</head>
<body>
<div id="message">Some text</div>
</body>
</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:

window.location.href = 'test.html?hello';

In test.html:

window.onload = function() {
  document.getElementById("message").innerHTML = window.location.search.substr(1);
}
泼猴你往哪里跑 2024-11-07 12:27:13

你必须编写

函数 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

document.getElementById("message")="hello";

而不是你的 html 中

you have to write in test.htm

document.getElementById("message")="hello";

not in your html

谎言 2024-11-07 12:27:13

您尝试设置为“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文