为什么这不起作用?我做错了什么?

时间:2014-06-06 15:36:43

标签: 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: 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>

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

也许我明白了。

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

答案 2 :(得分: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>