87,996
社区成员




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
<script type="text/javascript">
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
window.onload = function() {
var inputTest = document.getElementById('inputTest');
if (isIE) {
inputTest.attachEvent('onpropertychange', valueChangeHandler);
} else {
// Firefox下,只有DOM第2级事件才有效,DOM第0级无效
inputTest.addEventListener('input', function() {
valueChangeHandler();
}, false);
}
};
function valueChangeHandler() {
var inputTest = document.getElementById('inputTest');
var consoleDiv = document.getElementById('console');
var messageDiv = document.createElement('div');
messageDiv.innerHTML = inputTest.value;
consoleDiv.appendChild(messageDiv);
}
</script>
</head>
<body>
<input id="inputTest" type="text">
<div id="console"></div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Insert title here</title>
<script type="text/javascript">
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
window.onload = function() {
var inputTest = document.getElementById('inputTest');
if (isIE) {
inputTest.onpropertychange = valueChangeHandler;
} else {
if (inputTest.watch) {
inputTest.watch("value", function(id, oldVal, newVal) {
valueChangeHandler();
})
} else {
inputTest.oninput = valueChangeHandler;
}
}
};
function valueChangeHandler() {
var inputTest = document.getElementById('inputTest');
var consoleDiv = document.getElementById('console');
var messageDiv = document.createElement('div');
messageDiv.innerHTML = inputTest.value;
consoleDiv.appendChild(messageDiv);
}
</script>
</head>
<body>
<input id="inputTest" type="text">
<div id="console"></div>
</body>
</html>
keydown获得值是前一次输入的值,是键盘按下的时候触发。只有键盘松开的时候
才能获取当前输入的值,建议用keyup、keypress; lz就不要纠结在这个问题上了
而且ls解释都比较好
, <body>, <button>,, , ,
, , ,,, <em