【问题标题】:Why does this not work? What have I done wrong? [closed]为什么这不起作用?我做错了什么? [关闭]
【发布时间】:2014-07-27 22:50:01
【问题描述】:

为什么下面的段落不起作用?

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

【问题讨论】:

  • 什么意思?
  • 下面的段落应该是返回输入字段的长度,但它不起作用
  • “你能找到错误”问题are not good questions for Stack Overflow。确保您提供一个简短但对问题的具体说明,准确地告诉我们哪里出了问题。 “它不起作用”不是问题陈述。

标签: javascript html css


【解决方案1】:

我没有错误地分享此代码。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      也许我明白了。

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

      【讨论】:

      • 输入元素没有(合理的)innerHTML 值,它们被定义为EMPTY,所以不能有子节点。
      • @Quentin 是的。已编辑。
      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 2012-11-07
      • 2013-02-08
      • 2019-07-31
      • 1970-01-01
      • 2012-11-19
      • 2017-12-13
      • 2014-02-16
      相关资源
      最近更新 更多