Home > Article > Web Front-end > JavaScript method to implement customized hover and focus effects for input and textarea_javascript skills
本文实例讲述了JavaScript实现为input与textarea自定义hover,focus效果的方法。分享给大家供大家参考。具体如下:
这里演示JavaScript为input输入框和textarea文本框自定义hover,focus效果,hover也就是鼠标放上去之后的效果,focus是鼠标焦点问题,要实现 这种效果,需要JS来配合,这个例子就是很不错的,它把网页上输入框和文本框都加入了鼠标悬停和鼠标焦点效果。
运行效果截图如下:
在线演示地址如下:
http://demo.jb51.net/js/2015/js-input-textarea-hover-focus-style-codes/
具体代码如下:
<title>JavaScript为input/textarea自定义hover,focus效果</title> <script type="text/javascript"> function suckerfish(type, tag, parentId) { if (window.attachEvent) { window.attachEvent("onload", function() { var sfEls = (parentId==null)?document.getElementsByTagName(tag):document.getElementById(parentId).getElementsByTagName(tag); type(sfEls); }); } } sfHover = function(sfEls) { for (var i=0; i < sfEls.length; i++) { sfEls[i].onmouseover=function() { this.className+=" iptHover"; } sfEls[i].onmouseout=function() { this.className=this.className.replace(new RegExp(" iptHover\\b"), ""); } } } sfFocus = function(sfEls) { for (var i=0; i < sfEls.length; i++) { sfEls[i].onfocus=function() { this.className+=" iptFocus"; } sfEls[i].onblur=function() { this.className=this.className.replace(new RegExp(" iptFocus\\b"), ""); } } } </script> <style type="text/css"> textarea{ border:1px solid #BBE1F1; width:250px; height:80px; } .iptHover,input:hover,textarea:hover{ border:1px solid #77C2E3; } .iptFocus,input:focus,textarea:focus{ border:1px solid #77C2E3; background-color:#EFF7FF; } </style> <input type="text" name="textfield" /><br /> <textarea name="textarea"></textarea> <script type="text/javascript"> suckerfish(sfHover, "input"); suckerfish(sfFocus, "input"); suckerfish(sfHover, "textarea"); suckerfish(sfFocus, "textarea"); </script>
希望本文所述对大家的javascript程序设计有所帮助。