【发布时间】:2021-05-18 11:23:24
【问题描述】:
我刚刚回到Web Dev(在学习另一种语言之后),我只是想到了一个项目,一个文本键盘(当鼠标点击所需的字母时,它将它添加到一个字符串中),但是JS代码无法正常工作。
我只是在查看是否可以更改 <p> 标记的值,但它没有更改,或者我应该说出现。
a{
color: inherit;
text-decoration: none;
}
*{
margin:0;
padding:0;
}
#heading{
color: white;
text-align: center;
font-size: 24px;
font-weight: 420 bold;
font-family: sans-serif;
text-shadow: 2px 2px red;
margin-right: 10px;
margin-top: 20px;
}
#description{
color: green;
text-align: center;
font-size: 19px;
font-weight: 69 bold;
font-family: serif;
text-shadow: 1px 1px white;
margin-right: 10px;
margin-top: 20px;
}
#field{
color: white;
}
#testSubject{
width: 100px;
height: 30px;
}
<!DOCTYPE html>
<html style="background-color:black; margin:0;padding:0;" lang="en-US">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<meta name="description" content="The text keyboard provides the user to write text and copy it without the use of keyboard!"/>
<title>Text Keyboard | website.com</title>
<style>
</style>
</head>
<body>
<h1 id="heading">
Text Keyboard
</h1>
<p id="description">Just press the alphabet you want and see the magic!</p>
<div id="keyboard">
<p id="field"></p>
</script>
<button type="button" onclick="change()" value="Click Me!" id="testSubject">
</div>
<!--Always add the script on the bottom of the BODY tag since it contains the script-->
<script type="text/javascript">
function change(){
document.getElementById("field").value = "This changed just now!";
}
</script>
</body>
</html>
,当我尝试打印 Field 标签的 value 时,它会打印出文本,但在浏览器上,没有任何变化。我是否跳过了一些细节或什么,因为即使在阅读并尝试了 StackOverflow 上的其他问题之后,也没有任何改变。
请帮忙。
【问题讨论】:
-
嗨,在 JS 中使用
innerHTML而不是value。 -
只有输入元素有
.value属性。 -
根据经验,尽可能使用
textContent而不是innerHTML,因为后者会使您的应用容易受到Cross-site Scripting Attacks 的攻击。
标签: javascript html css browser getelementbyid