因为浏览器对文本框等元素的focus不支持,下面的代码就是事例
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
xml:lang
=
"
en
"
lang
=
"
en
"
>
<
head
>
<
style type
=
"
text/css
"
>
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
form
...
{
margin: 0px auto;
width: 450px;
border: solid 1px #CCCCCC;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
.bo
...
{
border-bottom: solid 1px #CCCCCC;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
label
...
{
float: left;
padding: 10px 0px 4px 30px;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
input
...
{
padding: 1px;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
input,textarea
...
{
border: 1px solid #CCCCCC;
margin: 5px 0px;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
textarea
...
{
padding: 2px;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
.bt
...
{
width: 38px;
height: 20px;
font-size: 11px;
border: solid 1px #CCCCCC;
background: #FBFBFB;
text-align: center;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
.btcenter
...
{
text-align: center;
clear: left;
padding: 4px 0px 0px;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
.sffocus ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
background: #F0F9FB; /**//*----for IE----*/
border: 1px solid #1D95C7;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
textarea:focus, input:focus ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
background: #F0F9FB; /**//*----for firefox......----*/
border: 1px solid #1D95C7;
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
body
...
{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #666666;
margin-top: 20px;
}
</
style
>
<
script type
=
"
text/javascript
"
>
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
function suckerfish(type, tag, parentId)
...
{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (window.attachEvent) ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
window.attachEvent("onload", function() ...{
var sfEls = (parentId==null)?document.getElementsByTagName(tag):document.getElementById(parentId).getElementsByTagName(tag);
type(sfEls);
});
}
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
sfHover
=
function(sfEls)
...
{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i=0; i<sfEls.length; i++) ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sfEls[i].onmouseover=function() ...{
this.className+=" sfhover";
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sfEls[i].onmouseout=function() ...{
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}
}
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
sfFocus
=
function(sfEls)
...
{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i=0; i<sfEls.length; i++) ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sfEls[i].onfocus=function() ...{
this.className+=" sffocus";
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sfEls[i].onblur=function() ...{
this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
}
}
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
sfActive
=
function(sfEls)
...
{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i=0; i<sfEls.length; i++) ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sfEls[i].onmousedown=function() ...{
this.className+=" sfactive";
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sfEls[i].onmouseup=function() ...{
this.className=this.className.replace(new RegExp(" sfactive\\b"), "");
}
}
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/None.gif)
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedBlockStart.gif)
sfTarget
=
function(sfEls)
...
{
var aEls = document.getElementsByTagName("A");
document.lastTarget = null;
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var i=0; i<sfEls.length; i++) ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (sfEls[i].id) ...{
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (location.hash==("#" + sfEls[i].id)) ...{
sfEls[i].className+=" sftarget";
document.lastTarget=sfEls[i];
}
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (var j=0; j<aEls.length; j++) ...{
if (aEls[j].hash==("#" + sfEls[i].id)) aEls[j].targetEl = sfEls[i];
![](/CuteSoft_Client/CodeHighlighter/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
aEls[j].onclick = function() ...{
if (document.lastTarget) document.lastTarget.className = document.lastTarget.className.replace(new RegExp(" sftarget\\b"), "");
if (this.targetEl) this.targetEl.className+=" sftarget";
document.lastTarget=this.targetEl;
return true;
}
}
}
}
}
suckerfish(sfHover, "p");
suckerfish(sfActive, "p");
suckerfish(sfHover, "INPUT");
suckerfish(sfActive, "TEXTAREA");
suckerfish(sfFocus, "INPUT");
suckerfish(sfFocus, "TEXTAREA");
suckerfish(sfTarget, "H5");
suckerfish(sfHover, "pre");
suckerfish(sfActive, "pre");
</
script
></
head
>
<
body
>
<
form action
=
"
#
"
method
=
"
post
"
name
=
"
myform
"
id
=
"
myform
"
>
<
div
class
=
"
bo
"
><
label
for
=
"
name
"
>
姓名:
</
label
>
<
input type
=
"
text
"
size
=
"
20
"
name
=
"
name
"
id
=
"
name
"
maxlength
=
"
10
"
/></
div
>
<
div
class
=
"
bo
"
><
label
for
=
"
tel
"
>
电话:
</
label
>
<
input type
=
"
text
"
size
=
"
20
"
name
=
"
tel
"
id
=
"
tel
"
maxlength
=
"
16
"
/></
div
>
<
div
class
=
"
bo
"
><
label
for
=
"
mo
"
>
主题:
</
label
>
<
input type
=
"
text
"
size
=
"
30
"
name
=
"
mo
"
id
=
"
mo
"
maxlength
=
"
20
"
/></
div
>
<
div
class
=
"
bo
"
><
label
for
=
"
con
"
>
内容:
</
label
>
<
textarea cols
=
"
40
"
rows
=
"
8
"
name
=
"
con
"
id
=
"
con
"
></
textarea
></
div
>
<
div
class
=
"
btcenter
"
><
input type
=
"
submit
"
class
=
"
bt
"
value
=
"
Send
"
/>
<
input type
=
"
reset
"
class
=
"
bt
"
value
=
"
Reset
"
/></
div
></
form
></
body
></
html
>
文章来源:
http://link-to.cn/post/2007/12/div-css-用focus实现p、h5、pre、文本框高亮效果.aspx