gpt4 book ai didi

javascript - 登录屏幕页面的getter和setter

转载 作者:行者123 更新时间:2023-11-28 03:24:03 25 4
gpt4 key购买 nike

谁能帮帮我?基本上我需要一个页面,在我网站的第一页上询问用户名。这将允许您进入主页。在主页被覆盖的那一刻,我如何在主页上使用此人的姓名,而只有一个白页,其中包含此人之前在屏幕上输入的内容?

登录页面代码

<link href="CSS.css" rel="stylesheet" type="text/css" />
<div id="image">
<img src="../images/logo.jpg"/>
</div>
<div id="login">
<script type="text/javascript">

// Called on form's `onsubmit`
function tosubmit() {
// Getting the value of your text input
var mytext = document.getElementById("mytext").value;

// Storing the value above into localStorage
localStorage.setItem("mytext", mytext);

return true;
}

</script>
</head>
<body>
<center>
<!-- INLCUDING `ONSUBMIT` EVENT + ACTION URL -->
<form name="myform" onsubmit="tosubmit();" action="index.html">

<input id="mytext" type="text" name="data">
<input type="submit" value="Submit">

</form>
</div>
</body>
</html>

首页代码

<script>

// Called on body's `onload` event
function init() {
// Retrieving the text input's value which was stored into localStorage
var mytext = localStorage.getItem("mytext");

// Writing the value in the document
document.write(" "+mytext);
}

</script>


<body onload="init();">

</body>

最佳答案

页面正在被 document.write 覆盖.你不应该使用它,查看warning在规范中:

Warning! This method has very idiosyncratic behavior. In some cases, this method can affect the state of the HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document (e.g. if the string written is the string "<plaintext>" or "<!--"). In other cases, the call can clear the current page first, as if document.open() had been called. In yet more cases, the method is simply ignored, or throws an exception. To make matters worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.

你的情况是这样的:

In other cases, the call can clear the current page first, as if document.open() had been called.

为避免这种情况,您可以使用 DOM 方法:

document.body.appendChild(document.createTextNode(mytext));

关于javascript - 登录屏幕页面的getter和setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22388728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com