---
title: "css中behavior属性用法说明"
author: "q272156430"
date: "2010-11-29T15:50:04Z"
tags: []
categories: ["Web前端"]
slug: "css-behavior"
draft: false
lastmod: "2010-11-29T15:50:04Z"
id: 828317
url: "https://xiaolele.iteye.com/blog/828317"
---

css中behavior属性用法说明:  
我们经常会遇到在刷新表单的时候,表单中的内容就丢失了,我们常用的方法是使用cookie,但那样有点麻烦,在css中有behavior属性可以为我们解决这个问题。下面就介绍一下behavior这个属性的用法:  
behavior的语法：  
behavior : url ( url ) | url ( #objID ) | url ( #default#behaviorName )  
取值：  
url ( url )　:　使用绝对或相对 url 地址指定DHTML行为组件（.htc）  
url ( #objID )　:　使用二进制实现(作为 ActiveX? 控件)的行为。此处的 #objID 为 object 对象的 id 属性值  
url ( #default#behaviorName )　:　IE的默认行为。由行为的名称标识( #behaviorName )指定  
说明：设置或检索对象的DHTML行为。多个行为之间用空格隔开。  
当多个附加到同一要素的行为发生冲突时，其结果取决于行为应用于要素的顺序。后一个行为的优先权高于前一个行为。同样的规则适用于不同行为提供的属性、事件、方法所发生的名称冲突。  
可以使用 addBehavior 方法动态的将行为附着到对象。  
此属性对于 currentStyle 对象而言是只读的。对于其他对象而言是可读写的。  
务必注意使用样式表(CSS)的 behavior 属性内联定义或使用 addBehavior 方法附着的行为不会在对象从文档树中移除时被自动分离。而在文档中的样式表规则定义的行为会在对象从文档树中移除时被自动分离。  
对应的脚本特性为 behavior 。  
实例：  
p { behavior: url(#default#download); }  
div { behavior: url(fly.htc) url(shy.htc); }  
div { behavior: url(#myObject); }  
应用1:刷新也保留输入框里面的文字  
.sHistory {}{behavior:url(#default#savehistory);}  
应用2:调用脚本  
&lt;html&gt;  
&lt;head&gt;  
&lt;style&gt;  
　　　h1 { behavior: url(behave.htc) }  
　　　&lt;/style&gt;  
&lt;/head&gt;  
&lt;body&gt;  
　　　　&lt;h1&gt;把鼠标放在这里 http://www.phpzixue.cn/&lt;/h1&gt;  
&lt;/body&gt;  
&lt;/html&gt;  
　　behave.htc  
&lt;component&gt;  
&lt;attach for="element" event="onmouseover" handler="hig\_lite" /&gt;  
&lt;attach for="element" event="onmouseout" handler="low\_lite" /&gt;  
&lt;script type="text/javascript"&gt;  
function hig\_lite()  
{  
　　　element.style.color=255  
}  
function low\_lite()  
{  
　　　element.style.color=0  
}  
&lt;/script&gt;  
&lt;/component&gt;　　  
注意：CSS 时尽量避免使用Behaviors技术  
Internet Explorer Behaviors  
它是什么？Internet Explorer 5 引入了行为 (behaviors)。behaviors 是一种通过使用 CSS 向 HTML 元素添加行为的方法。  
只有 Internet Explorer 支持 behavior 属性。  
尽量使用 JavaScript 和 HTML DOM 代替它.

 

\*.htc

 

&lt;PUBLIC:COMPONENT ID="summerSelectObj" lightWeight="false" literalContent="true" NAME="summerSelectObj" supportsEditMode="false"&gt;  
&lt;PUBLIC:DEFAULTS canHaveHTML="true" contentEditable="false" tabStop="true"/&gt;  
&lt;PUBLIC:PROPERTY ID="propData" NAME="data" PUT="setData" GET="getData"/&gt;  
&lt;PUBLIC:attach  event="ondetach" onevent="cleanup()" /&gt;  
&lt;PUBLIC:attach event="oncontentready" onevent="fnInit()"/&gt;  
&lt;PUBLIC:attach event="onfocus" onevent="fnMrShow()"/&gt;  
&lt;PUBLIC:attach event="onblur" onevent="fnBlur()"/&gt;  
&lt;PUBLIC:attach event="onclick" onevent="fnMrShow()"/&gt;  
&lt;PUBLIC:attach event="onkeydown" onevent="fnKeyDown()"/&gt;  
&lt;PUBLIC:attach event="onchange" onevent="fnOnchange()"/&gt;  
&lt;PUBLIC:method   name="setValue"/&gt;  
&lt;PUBLIC:method   name="setData"/&gt;  
&lt;PUBLIC:method   name="fnFind"/&gt;  
&lt;PUBLIC:method   name="fnInit"/&gt;  
&lt;PUBLIC:method   name="fnIsClick"/&gt;  
&lt;PUBLIC:method   name="fnGetLstStr"/&gt;  
&lt;PUBLIC:method   name="fnMySort0"/&gt;  
&lt;PUBLIC:method   name="fnMySort"/&gt;  
&lt;PUBLIC:method   name="fnMySort2"/&gt;  
&lt;PUBLIC:method   name="fnSortShow"/&gt;  
&lt;PUBLIC:method   name="fnShowPopWin"/&gt;  
&lt;PUBLIC:method   name="fnPropertyChange"/&gt;  
&lt;PUBLIC:method   name="fnKeyDown"/&gt;  
&lt;PUBLIC:method   name="fnClick"/&gt;  
&lt;PUBLIC:method   name="fnAddFilter"/&gt;  
&lt;PUBLIC:method   name="fnGetDescAndHidObj"/&gt;  
&lt;PUBLIC:method   name="fnGetValue"/&gt;  
&lt;PUBLIC:method   name="fnInitXml"/&gt;  
&lt;PUBLIC:method   name="fnAjaxCb"/&gt;  
&lt;PUBLIC:method   name="setInputRegExp"/&gt;  
&lt;PUBLIC:method   name="createXMLHttpRequest"/&gt;  
&lt;PUBLIC:method   name="fnGetAjaxData"/&gt;  
&lt;PUBLIC:attach event="onpropertychange" onevent="fnPropertyChange()"/&gt;