JS文件: function mouseAction() { var textInputs = document.getElementsByTagName("input"); var len = textInputs.length; var index = 0; var textInput; /* 也能用 for in 语句遍历 for (textInput in textInputs){ textInputs[textInput].onmouseover = functionName; } */ for( index = 0; index < len; index++ ) { textInput = textInputs[index]; if( textInput.getAttribute("type") == "text" ){ textInput.onmouseover = function (){ //也能用这种方式 this.style.backgroundColor = "red"; this.className = "txtMouseOver"; //要先在HTML中引入CSS文件 }; //注意要加分号 textInput.onmouseout = function(){ this.className = "txtMouseOut"; };