1.一个普通的input元素,在不被 form包裹的时候,如何跳转或搜索
var oInput = document.getElementsByTagName('input')[0]; oInput.addEventListener('keyup',function(event){ var event = event || window.event; if(event.keyCode === 13) { location.href = 'http://www.baidu.com/s?wd=' + this.value; } })
2.多个input按enter输入下一项
document.addEventListener('DOMContentLoaded', function () { var oInputs = document.getElementsByTagName('input'); var len = oInputs.length; for (var i = 0; i < len; i++) { console.log(i); (function (i) { oInputs[i].addEventListener('keydown', function (event) { event = event || window.event; console.log(event.keyCode); if (this.type != 'submit' && event.keyCode === 13) { event.preventDefault(); oInputs[i + 1].focus(); } }) })(i) } })