代码
提交代码
<style> input { border: 1px solid #eee; padding: 4px 8px; min-width: 300px; font-size: 14px; height: 40px; display: block; margin: 0 auto; outline: none; } .result { text-align: center; } </style> <div> <input type="text"> <div class="result"></div> </div> <script> var input = document.querySelector('input'); var result = document.querySelector('.result'); var timer = null; input.addEventListener('input', function(e) { clearTimeout(timer); timer = setTimeout(function() { var val = e.target.value; result.innerText = '联想内容:' + val; }, 300); }); </script>
运行结果