cssup-banner.jpg

Internet Explorer Behaviors

它是什麼?Internet Explorer 5引入了行為(behaviors)。behaviors是一種通過使用CSS向HTML元素添加行為的方法。

為什麼要避免它?只有Internet Explorer支持behavior屬性。

用什麼代替?請使用JavaScript和HTML DOM取而代之。


例子1 - Mouseover Highlight

下面的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 和事件句柄。


例子2 - Typewriter Simulation

下面的HTML 文件中有一個<style> 元素,它為id 為"typing" 的元素定義了一個行為:

<html>
<head>
<style type="text/css">
#typing
{
behavior:url(behave_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 elements
with the ease of CSS.<br /><br />How do behaviors work?<br />
By using XML we can link behaviors to any element in a web page
and manipulate that element.</p>
</span>
</body>
</html>
	

下面是XML 文檔"behave.htc":

<attach for="window" event="onload" handler="beginTyping" />
<method name="type" />
<script type="text/javascript">
var i,text1,text2,textLength,t;
function beginTyping()
{
i=0;
text1=element.innerText;
textLength=text1.length;
element.innerText="";
text2="";
t=window.setInterval(element.id+".type()",speed);
}
function type()
{
text2=text2+text1.substring(i,i+1);
element.innerText=text2;
i=i+1;
if (i==textLength)
  {
  clearInterval(t);
  }
}
</script>
	
arrow
arrow

    設計密碼工作室 發表在 痞客邦 留言(0) 人氣()