文本输入大于2后,使div可见

时间:2017-12-13 12:58:20

标签: javascript html css function input

我创建了一个函数,用于在文本长度大于2时显示div。但它似乎不起作用。

jsfiddle:https://jsfiddle.net/bj22met8/

的javascript

function showDIV(){
 text = document.getElementById("search");
    if (text.length >=1){
        document.getElementById("section").style.visibility = 
  "visible";
    }

  }

HTML

<div id="section">
  dsfdsf
  </div>
  <input type="text" id="search" size="20" onkeydown="showDIV();">

 #section{
 width: 200px;
 height: 200px;
 background-color: blue;
 visibility: hidden;
 }

1 个答案:

答案 0 :(得分:4)

您需要测试输入值。 DOM对象(document.getElementById的结果)没有长度。

if (text.value.length >= 1) {...}