HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Input输入框实时监听例子(兼容IE8)</title>
</head>
<body>
<input type="text" id="search-input" />
<div id="text"></div>
<script>
var searchInput = document.getElementById("search-input");
var text = document.getElementById("text");
// 是IE浏览器(兼容IE8)
if (document.all) {
searchInput.onpropertychange = function () {
text.innerHTML = searchInput.value
}
}
// 非IE
else {
searchInput.addEventListener('input', function () {
text.innerHTML = searchInput.value
})
}
</script>
</body>
</html>
在线预览: