我从 JS 开始,并且在尝试编写的特定函数时遇到了麻烦。目的是在按下按钮后更改文本。“无”文本应变为“你打招呼,我打招呼?” 与嘿?是文档上已有的文本。
这是代码,欢迎任何指针!
<html>
<head>
<title>Javascript test </title>
</head>
<body>
<p id="hey">Hey?</p>
<p id ="nothing">Nothing</p>
<INPUT type =button VALUE="Button?" OnClick=(take())>
<script>
function take(){
var hey=document.getElementById("hey");
var nothing =document.getElementById("one");
nothing.appendChild(document.createTextNode("You say hi, I say " + hey));
}
</script>
</body>
</html>
好的,谢谢大家的帮助。没有人是 100% 正确的,但我尝试了所有建议的组合,并获得了大约 99% 的方法,到达了我想去的地方。我的代码现在看起来像这样。我试图做的最后一个改变不是追加新的文本字符串,而是用当前正在追加的字符串替换旧字符串。
<html>
<head>
<title>Javascript test </title>
</head>
<body>
<p id="hey">Hey?</p>
<p id ="nothing">Nothing</p>
<input type ="button" value="Button?" OnClick="take();"/>
<script>
function take(){
var hey=document.getElementById("hey");
var nothing =document.getElementById("nothing");
nothing.appendChild(document.createTextNode("You say hi, I say " + hey.innerHTML));
}
</script>
</body>
</html>
感谢您的帮助,感谢 Stack Overflow 社区!