原因之5—易于键盘操作 所有被Internet Explorer认为是“控件”的元素都可以分得一个叫做ACCESSKEY的HTML属性,分配的方法如下: < TAGNAME ACCESSKEY=keystring> keystring的值识别元素的“快捷访问键”。按下Alt和这个键可以将焦点集中在元素上,或者点击元素。 接受 ACCESSKEY 属性的元素有: A, APPLET, BUTTON, EMBED, FIELDSET, IFRAME, IMG, INPUT, LABEL, LEGEND, OBJECT, SELECT, TABLE, TEXTAREA 举例来说,如果我们的页面包含这样的HTML: < BUTTON ACCESSKEY="s" onClick="stopIt()">Stop< /BUTTON> 我们就可以在键盘上使用Alt-S来执行stopIt()函数。 在显示的文本中,我们在与键盘相应的字母下面增加下划线,从而提醒用户这个功能。请点击这里看一下效果。 实现这个效果的具体HTML和脚本如下: < HTML> < HEAD> < STYLE TYPE="text/css"> .but { border:1px buttonface solid; font-family:MS Sans Serif;font-size:8pt; } < /STYLE> . . . < /HEAD> < BODY> . . . < DIV ID="toolbar" NOWRAP STYLE="width:20;background-color:buttonface;padding:2px;"> < BUTTON CLASS=but ACCESSKEY="s"> onClick="yourStopFunction()"< IMG SRC="StopOff.gif" WIDTH=19 HEIGHT=20 BORDER=0>< BR> < U>S< /U>top< /BUTTON>< BUTTON CLASS=but ACCESSKEY="r" onClick="yourRefreshFunction()"IMG SRC="RefreshOff.gif" WIDTH=17 HEIGHT=20 BORDER=0>< BR> < U>R< /U>efresh< /BUTTON>< BUTTON CLASS=but ACCESSKEY="h" onClick="yourHomeFunction()"IMG SRC="HomeOff.gif" WIDTH=19 HEIGHT=20 BORDER=0>< BR> < U>H< /U>ome< /BUTTON>< BUTTON CLASS=but ACCESSKEY="e" onClick="yourSearchFunction()"IMG SRC="SearchOff.gif" WIDTH=20 HEIGHT=20 BORDER=0>< BR> S< U>e< /U>arch< /BUTTON>< BUTTON CLASS=but ACCESSKEY="f" onClick="yourFavoritesFunction()"IMG SRC="FavoritesOff.gif" WIDTH=20 HEIGHT=20 BORDER=0>< BR> < U>F< /U>avorites< /BUTTON>< BUTTON CLASS=but ACCESSKEY="i" onClick="yourHistoryFunction()"IMG SRC="HistoryOff.gif" WIDTH=20 HEIGHT=20 BORDER=0>< BR> H< U>i< /U>story< /BUTTON> < /DIV> . . . < SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript"> allBUTs = toolbar.children; maxWidth = 0; for (i=0;i< allBUTs.length;i++) { tSpan = allBUTs(i); tSpan.onselectstart = function(){return false} maxWidth = Math.max(maxWidth,tSpan.offsetWidth); tSpan.img = tSpan.children(0); tSpan.oversrc = tSpan.innerText + "On.gif"; tSpan.outsrc = tSpan.innerText + "Off.gif"; tSpan.onmouseover = function(){ this.style.border = "1px buttonhighlight outset"; this.img.src = this.oversrc; } tSpan.onmouseout = function(){ this.style.border = "1px buttonface solid"; this.img.src = this.outsrc; } tSpan.onmousedown = function(){ this.style.border = "1px buttonhighlight inset"; } tSpan.onmouseup = function(){ this.style.border = "1px buttonhighlight outset"; window.focus(); } } for (i=0;i< allBUTTONs.length;i++) { tSpan = allBUTTONs(i); tSpan.style.pixelWidth = maxWidth; } < /SCRIPT>> < /BODY> < /HTML> 最后,我们开始包装整个程序。 程序包装 使用简单的DHTML,Internet Explorer就能够使你更容易地为应用程序创建象Windows界面中看到的工具栏。本文中,我们既创建了简单的基于文本的工具栏,又创建了一个较为复杂的,使用了图形、按钮、翻转和键盘操作的工具栏。点击下面的链接下载一个完整的工作实例压缩文件: toolbar.zip 。
|