2014-06-06 113 views
-7

为什么下面的段落不工作?为什么这不起作用?我做错了什么?

<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> 
+2

你是什么意思不起作用? –

+0

下面的段落应该返回输入字段的长度,但它不起作用 – Fizzy

+3

“你能找到bug”的问题[对于Stack Overflow来说不是很好的问题](http://meta.stackoverflow.com/questions/ 253787 /是,有正当-FIX-MY-代码吗?CB = 1#253788)。确保你提供了一个简短的,但**具体的问题**,准确地告诉我们什么是错的。 “它不起作用”不是问题陈述。 –

回答

0

也许我明白了。

function x() { 
     var textLength = document.getElementById("input1").value.length; 
     document.getElementById("textLength2").innerHTML = textLength; 
    } 
+1

输入元素没有(明智的)'innerHTML'值,它们被定义为'EMPTY',因此不能有子节点。 – Quentin

+0

@Quentin是的。编辑。 – nicael

5

document.getElementById("input1")给你一个输入元素,而不是一个字符串。您需要阅读其value属性。

0

我分享此代码没有错误。

<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> 
相关问题