Internet Explorer 5引入了DHTML行為。行為(Behaviors)是一種借助CSS的便利性向HTML元素添加DHTML功能性的方法。
IE 5引入了DHTML行為(DHTML behaviors)。行為(Behaviors)是一種借助CSS的便利性向HTML元素添加DHTML功能性的方法。
行為如何工作呢?通過使用XML,我們可以把行為鏈接到網頁中的任何元素,并對元素進行操作。
DHTML行為不使用<script>標簽,它們使用的是名為"behavior"的CSS屬性。"behavior"規定了指向某個HTC文件的URL,此文件含有實際的行為(HTC文件使用XML編寫)。
behavior: url(some_filename.htc)
注釋:behavior屬性被IE 5以及更高的版本支持,其他所有的瀏覽器都會忽略它。這意味著Mozilla、Firefox、Netscape以及其他瀏覽器只能識別常規的內容,而只有IE 5+可以看到DHTML behaviors。
下面的HTML文件擁有一個<style>元素,其中定義了針對<h1>元素的行為:
<html><head><style type="text/css">h1 { behavior: url(behave.htc) }
</style></head><body><h1>Mouse over me!!!</h1></body></html>
XML文檔"behave.htc"如下:
<attach for="element" event="onmouseover" handler="hig_lite" /><attach for="element" event="onmouseout" handler="low_lite" /><script type="text/javascript">function hig_lite(){element.style.color='red'}function low_lite(){element.style.color='blue'}</script>
behavior文件含有一段JavaScript,以及用于腳本的事件處理程序。
TIY (請把鼠標放到例子中的文本上面).
下面的HTML文件擁有一個<style>元素,其中定義了針對擁有id為"typing"的元素的行為:
<html><head><style type="text/css">#typing{behavior:url(typing.htc);
font-family:'courier new';}</style></head><body><span id="typing" speed="100">IE5 introduced DHTML behaviors.Behaviors are a way to add DHTML functionality to HTML elementswith the ease of CSS.<br /><br />How do behaviors work?<br />By using XML we can link behaviors to any element in a web pageand manipulate that element.</p></span></body></html>
XML文檔"typing.htc"如下:
<attach for="window" event="onload" handler="beginTyping" /><method name="type" /><script type="text/javascript">var i,text1,text2,textLength,tfunction beginTyping(){i=0text1=element.innerTexttextLength=text1.lengthelement.innerText=""text2=""t=window.setInterval(element.id+".type()",speed)}function type(){text2=text2+text1.substring(i,i+1)element.innerText=text2i=i+1if (i==textLength){clearInterval(t)}}</script>
新聞熱點
疑難解答