如何获取网页里的所有可获得焦点的控件
比如input,select,a
但是不要用document.forms[0].elements来获取
------解决方案--------------------一个网页只能有一个控件获取焦点!
------解决方案--------------------是相得到所有focus方法的对象吗?是不是以下的都上算:
A, ACRONYM, ADDRESS, APPLET, AREA, B, BDO, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CUSTOM, DD, DEL, DFN, DIR, DL, document, DT, EM, EMBED, FIELDSET, FONT, FORM, FRAME, FRAMESET, hn, HR, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, ISINDEX, KBD, LABEL, LEGEND, LI, LISTING, MARQUEE, MENU, OBJECT, OL, P, PLAINTEXT, PRE, Q, RT, RUBY, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, window, XMP
------解决方案--------------------有tabIndex属性的对象有:
A, ACRONYM, ADDRESS, APPLET, AREA, B, BDO, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CUSTOM, DD, DEL, DFN, DIR, DIV, DL, DT, EM, FIELDSET, FONT, FORM, FRAME, FRAMESET, hn, HR, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, ISINDEX, KBD, LABEL, LEGEND, LI, LISTING, MARQUEE, MENU, OBJECT, OL, P, PLAINTEXT, PRE, Q, RT, RUBY, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP
------解决方案-------------------- <html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> New Page 1 </title>
</head>
<body>
<b > fdsafd </b>
<div > fdsafd </div>
<input>
<a href= "# "> ddd </href>
<b > fdsafd </b>
<b > fdsafd </b>
<input type=button value=test onclick= "testFun(); ">
</a>
</body>
</html>
<script>
//这个方法可能会打乱页面的scrollbar
function testFun()
{
var bodyChildren=document.body.all;
var allFocusableObjs=new Array();
for(var i=0;i <bodyChildren.length;i++){
try{
bodyChildren[i].focus();
if(document.activeElement==bodyChildren[i]) allFocusableObjs.push(bodyChildren[i]);
}catch(e){}
}
alert(allFocusableObjs.length);
}
</script>
------解决方案--------------------可以试一下:
<td onfocus= "alert(1) "> ddd </td>
不过它的focus表现形式跟input的不一样。
另外,
<input>
<div contentEditable=true >
<table >
<tr>
<td > dddd </td>
</tr>
</table>
</div>
<input>
------解决方案--------------------~
------解决方案--------------------如果只是要简单的几个对象,可以用下面这个语法:
var a = document.getElementsByTagName( "input ");
for (var i=0; i <a.length; i++) {
//a[i].value;
......
}