如题所述
总共两个文件,将他们存到同一目录下,然后访问index.html文件即可以看到效果
文件一:caisewenzi.htc
<PUBLIC:COMPONENT URN="rainbow" name="darainbow()"/>
<PUBLIC:ATTACH EVENT = "oncontentready" ONEVENT ="beginIt()"/>
<SCRIPT language="JAVASCRIPT">
var Kill_ID=null;
var internalValue;
var originalValue;
function putValue(){
element.innerHTML=internalValue;
encapsulate();
startSwirl();
}
function eventHandler(){
clearInterval(Kill_ID);
internalValue=element.value;
element.detachEvent("onpropertychange",eventHandler);
if(event.propertyName=="value"){
element.innerHTML=internalValue;
putValue();
} else startSwirl();
element.attachEvent("onpropertychange",eventHandler);
}
function swirl(){
var I;
I=ran(internalValue.length-1,0);
if(element.children(I) != null)element.children(I).style.color=randomColor();
}
function startSwirl(){
if (element.swirlInterval == null)
element.swirlInterval=50;
if ((internalValue!=null) && (element.swirlInterval!=0))
Kill_ID=setInterval(swirl,element.swirlInterval);
}
function encapsulate(){
var I;
var tempStr="";
for (I=0; I < internalValue.length; I++){
tempStr += "<DIV style=\"color:"+randomColor()+"\">" + internalValue.substr(I,1) + "</DIV>";
}
innerHTML=tempStr;
}
function ran(upperbound, lowerbound){
return(parseInt((upperbound - lowerbound + 1) * Math.random() + lowerbound));
}
function randomColor(){
var R=ran(255,0);
var G=ran(255,0);
var B=ran(255,0);
R=R.toString(16);
G=G.toString(16);
B=B.toString(16);
if (R.length==1)R="0"+R;
if (G.length==1)G="0"+G;
if (B.length==1)B="0"+B;
return("#"+R+G+B);
}
function beginIt(){
internalValue=element.value;
originalValue=element.innerHTML;
if (internalValue==null)
internalValue=element.innerHTML;
putValue();
element.attachEvent("onpropertychange",eventHandler);
}
function clearIt(){
clearInterval(Kill_ID);
element.detachEvent("onpropertychange",eventHandler);
element.innerHTML=originalValue;
}
</script>
文件二:index.html
<html>
<head><title>彩色文字演示</title>
<style>
.flashwords{behavior:url("caisewenzi.htc");}
</style>
</head>
<body>
<span class="flashwords">彩色文字</span>
</body>
</html>
文件一:caisewenzi.htc
<PUBLIC:COMPONENT URN="rainbow" name="darainbow()"/>
<PUBLIC:ATTACH EVENT = "oncontentready" ONEVENT ="beginIt()"/>
<SCRIPT language="JAVASCRIPT">
var Kill_ID=null;
var internalValue;
var originalValue;
function putValue(){
element.innerHTML=internalValue;
encapsulate();
startSwirl();
}
function eventHandler(){
clearInterval(Kill_ID);
internalValue=element.value;
element.detachEvent("onpropertychange",eventHandler);
if(event.propertyName=="value"){
element.innerHTML=internalValue;
putValue();
} else startSwirl();
element.attachEvent("onpropertychange",eventHandler);
}
function swirl(){
var I;
I=ran(internalValue.length-1,0);
if(element.children(I) != null)element.children(I).style.color=randomColor();
}
function startSwirl(){
if (element.swirlInterval == null)
element.swirlInterval=50;
if ((internalValue!=null) && (element.swirlInterval!=0))
Kill_ID=setInterval(swirl,element.swirlInterval);
}
function encapsulate(){
var I;
var tempStr="";
for (I=0; I < internalValue.length; I++){
tempStr += "<DIV style=\"color:"+randomColor()+"\">" + internalValue.substr(I,1) + "</DIV>";
}
innerHTML=tempStr;
}
function ran(upperbound, lowerbound){
return(parseInt((upperbound - lowerbound + 1) * Math.random() + lowerbound));
}
function randomColor(){
var R=ran(255,0);
var G=ran(255,0);
var B=ran(255,0);
R=R.toString(16);
G=G.toString(16);
B=B.toString(16);
if (R.length==1)R="0"+R;
if (G.length==1)G="0"+G;
if (B.length==1)B="0"+B;
return("#"+R+G+B);
}
function beginIt(){
internalValue=element.value;
originalValue=element.innerHTML;
if (internalValue==null)
internalValue=element.innerHTML;
putValue();
element.attachEvent("onpropertychange",eventHandler);
}
function clearIt(){
clearInterval(Kill_ID);
element.detachEvent("onpropertychange",eventHandler);
element.innerHTML=originalValue;
}
</script>
文件二:index.html
<html>
<head><title>彩色文字演示</title>
<style>
.flashwords{behavior:url("caisewenzi.htc");}
</style>
</head>
<body>
<span class="flashwords">彩色文字</span>
</body>
</html>
温馨提示:答案为网友推荐,仅供参考