gpt4 book ai didi

javascript - 为什么我在运行 javascript 后会丢失格式?

转载 作者:行者123 更新时间:2023-11-29 18:11:02 24 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,但为什么在函数 test() 启动时我会丢失所有格式?我应该在我的代码中更改什么?非常感谢您的帮助!

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
background: #E6E6FA;
font-family: book antiqua;
}
h1, h2 {
color: grey;
}
</style>

</head>
<h3>Title</h3>
<body bgcolor="#E6E6FA">
<input type="text" id="userInput"></input>
<button onclick="test()">Submit</button>
<p id="Demo"></p>
<p id="Beg"></p>
<p id="Fin"></p>
<script>
function test()
{
var nam= document.getElementById("userInput").value;
var l = nam.length;
var pocz = nam.slice(0,1);
var kon = nam.slice(-1);
document.getElementById("Demo").innerHTML = document.write("Your secret code: " + l + pocz + kon);
var one = nam.slice(-1)
if (one == "a") {
document.write(nam.slice(0,-1) + "bbb");
} else {
document.write(nam + "ccc");
}


}
</script>
</body>
</html>

最佳答案

如果 document.write 在 DOM 加载后 被调用,它会替换 文档。此外,您使用的 document.write 不正确,它不会返回任何内容。只需省略它,它就可以正常工作。

document.getElementById("Demo").innerHTML = "Your secret code: " + l + pocz + kon;

对于其他用途,做同样的事情并通过 innerHTML 将值分配给元素。

请阅读documentation在您使用不熟悉的功能之前。

关于javascript - 为什么我在运行 javascript 后会丢失格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27448762/

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