【GreaseMonkey脚本】让网页中的第一个文本框自动获得焦点

 很多网页往往没有专门把焦点设置在第一个文本框中,甚至我在FF中打开Baidu也有这样的问题,焦点竟然也不在文本框中(事实上我看了baidu的代码,onload事件是把焦点置到文本框中了,不过在FF中无效,可能是同我的其他 GreaseMonkey脚本冲突了),因此写了这个脚本方便输入。
// ==UserScript==
// @namespace     http://www.firefox.net.cn
// @name          Focus to First InputBox
// @description   Set focus to the first inputBox in the webpage
// @include       http://*
// ==/UserScript==
(function() {   
   window.addEventListener(
'DOMContentLoaded', setFocus(), false);

   function setFocus()
   {            
      var inputFields 
= document.getElementsByTagName('input');
      
      
if(inputFields)
      {
         var type 
= '';
         var len 
= inputFields.length;
         
         
for (i = 0; i < len; i++)
         {               
            type 
= inputFields[i].type ? inputFields[i].type.toLowerCase() : '';      
            
            
if ('text' == type && !inputFields[i].hasFocus)
            {         
               inputFields[i].focus();                        
               
return;
            }
         }
      }
   }
})();
 

 使用方法是把这段代码复制到一文本文件中,并保存为setfocus.user.js,然后用FF打开这个文件,安装GreaseMoneky的话,会提示你安装。

posted on 2006-06-06 14:45  Shadower  阅读(993)  评论(0)    收藏  举报

导航