Javascript 为什么这不起作用?我做错了什么?
为什么下面的段落不起作用Javascript 为什么这不起作用?我做错了什么?,javascript,html,css,Javascript,Html,Css,为什么下面的段落不起作用 <body onload="x()" background=" http://theworldsbestever.s3.amazonaws.com/blog/wp-content/uploads/2013/02/tumblr_inline_mi17k7Afai1qz4rgp.gif"> <font size="32%"><h1 style="font-family: Verdana; color: red; text-align: cent
<body onload="x()" background=" http://theworldsbestever.s3.amazonaws.com/blog/wp-content/uploads/2013/02/tumblr_inline_mi17k7Afai1qz4rgp.gif"> <font size="32%"><h1 style="font-family: Verdana; color: red; text-align: center"><u>How long is your text?<u></font>
</h1>
<br>
<div style="text-align: center;">
<input style="text-align: center;" id="input1" maxlength="6" placeholder="Enter Some Text">
<br><br>
<button type="button" onclick="x()">Submit</button>
</div>
<p id="textLength2" style="color: white; text-align: center; font-family: verdana;"></p>
<script>
function x() {
var textLength = document.getElementById("input1");
var textLength = textLength.length;
document.getElementById("textLength2").innerHTML = textLength;
}
</script>
</body>
您的文本有多长?
提交
函数x(){
var textLength=document.getElementById(“input1”);
var textLength=textLength.length;
document.getElementById(“textLength2”).innerHTML=textLength;
}
也许我明白了
function x() {
var textLength = document.getElementById("input1").value.length;
document.getElementById("textLength2").innerHTML = textLength;
}
document.getElementById(“input1”)
提供一个输入元素,而不是字符串。您需要读取它的值
属性。我没有错误地共享此代码
<html>
<body onload="x()"
</h1>
<br>
<div style="text-align: center;">
<input style="text-align: center;" id="input1" maxlength="6" placeholder="Enter Some Text"> </input>
<br><br>
<button type="button" onclick="return x()">Submit</button>
</div>
<p id="textLength2" style="color: white; text-align: center; font-family: verdana;"></p>
<script>
function x() {
var text ;
text = document.getElementById('input1').value;
console.log(text);
var long = text.length;
document.getElementById("textLength2").innerHTML = text;
}
</script>
</body>
</html>
什么意思不起作用?下面的段落应该返回输入字段的长度,但它不起作用“你能找到bug吗”问题。确保你提供了一个简短但具体的问题陈述,准确地告诉我们哪里出了问题。“它不工作”不是一个问题。输入元素没有(合理的)innerHTML
值,它们被定义为EMPTY
,因此不能有子节点。@Quentin是的。编辑。