gpt4 book ai didi

javascript - 为什么不打印段落中的字符?

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

编写一个脚本,输入一个字符的整数代码并显示相应的字符。

它应该打印在一个段落中。

function character() {
var input = document.getElementById( "input" );
var code = document.getElementById( "output" ).innerHTML = input;
output.value = String.fromCharCode( code );
}
<input id="input" type="text" size="10">
<br>
<input type="button" value="click here" onclick="character()" id="button">
<br>
<p id="output" ></p>

最佳答案

A P 不是表单元素。此外,如果您使用 form.fieldName,则需要使用 name="" 而不是 id="";否则对所有字段使用 document.getElementById 并忽略表单。

您需要使用 innerHTML 或 innerText/textContent对于一个段落

document.getElementById("output").innerHTML=...

function character() {
var form = document.getElementById("form");
var code = parseInt(form.input.value,10); // radix 10 to make decimal
document.getElementById("output").innerHTML = String.fromCharCode(code);
}
<form id="form">
<input name="input" type="text" size="10">
<br>
<input type="button" value="click here" onclick="character()" id="button">
<br>
<p id="output" ></p>
</form>

关于javascript - 为什么不打印段落中的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558512/

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