为什么不打印段落中的字符
Why it is not printing the character in a paragraph?
编写一个脚本,为一个字符输入整数代码并显示相应的字符。
应该在一个段落中打印。
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>
P
不是表单元素。如果你使用形式。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>
在访问表单中的元素时遇到了问题。
document.getElementById("form").elements['input']
但是这只对表单元素有效,对其他HTML元素无效。
在这些元素中,您可以使用id或name,但由于历史原因。
你还有别的办法:
function character() {
var input = document.getElementById("input"),
output = document.getElementById("output");
output.innerHTML = String.fromCharCode(parseInt(input.value), 10) || '';
}
<form id="form">
<input id="input" type="text" size="10">
<br>
<input type="button" value="click here" onclick="character()" id="button">
<br>
<p id="output" ></p>
</form>
这就行了:
<script>
function character() {
var ChrCode = document.getElementById("input").value;
document.getElementById("output").innerText = ChrCode + " = " + String.fromCharCode(ChrCode);
//output.value = String.fromCharCode( code );
}
</script>
<input id="input" type="text" size="10">
<br>
<input type="button" value="click here" onclick="character()" id="button">
<br>
<p id="output" >Output</p>
相关文章:
- Javascript-在文本区域中断,但不在段落中中断
- 如何在javascript中更改段落中不同行的css样式
- 从一组具有相同起始和结束的所有段落的段落中提取包含特定字符串的段落
- 我怎么能在同一个HTML页面上多次在不同的段落中添加相同的字符串呢
- 如何等到动态内容加载到段落中
- 如何按从当前单词到段落中该句子(.)结尾的范围选择文本
- JavaScript Unicode Regex - 字符类中的顺序范围
- 单击“段落到文本区域”后,它会更新段落中的文本
- 如何在jQuery中从段落中删除不需要的元素
- 从文本框中获取文本并将其显示在段落中
- 将文本值与复选框值之和相乘,并将其显示在段落中
- 在JQuery mobile中的段落中使用变量
- 段落中的新行无效
- JavaScript在段落中的页面加载后添加文本
- "错误CS1012:字符文字“”中的字符太多;
- 对象不反映段落中的属性
- 如何为段落中的每个句子添加递增ID
- 如何使用jQuery从段落中获取相同字符的NUMBER
- 计算段落中的字符数
- 为什么不打印段落中的字符