Attaches a behavior to the element. dhtml语法 iid = object.addbehavior(surl)
parameters sUrl |
Required.
Stringthat specifies one of the following values: sValue | Location of the behavior, in URL format. The URL must be bounded by quotation marks, as illustrated in the code sample below. | #default#behaviorName | One of the Microsoft® Internet Explorer?A href="../reference.html">default behaviors, identified by its behavior name. | #objid | the id attribute specified in an object tag for the binary implementation of a behavior. |
|
return value returns an identifier that can be used later to detach the behavior from the element.
remarks this method, and the following remarks, apply only to attached behaviors, which are the original dynamic html(动态html,即dhtml) behaviors introduced in internet explorer 5. element behaviors are a new feature in internet explorer 5.5 and cannot be added or removed from an element. this method enables you to attach a behavior without using cascading style sheets (css). unless the behavior specified in the addbehavior call is one of the default behaviors built into internet explorer, the addbehavior call causes internet explorer to download the behavior asynchronously, before the behavior is attached to the element. due to the asynchronous nature of the addbehavior method, its return value cannot be relied on to determine whether the behavior was successfully applied to the element. waiting for the onreadystatechange event to fire and verifying that the readystate property of the element is set to complete ensure that the behavior is completely attached to the element, and that all the behavior's members are available for scripting. otherwise, attempting to use any behavior-defined member before the behavior is attached to the element results in a scripting error indicating that the object does not support that particular member.
Note A behavior attached to an element using the addBehavior method, or by applying the proposed CSS?A href="../properties/behavior_1.html">behavior attribute inline, is not automatically detached from the element when the element is removed from the document hierarchy. however, a behavior attached using a style rule defined in the document is detached automatically as the element is removed from the document tree.
DHTML代码范例
This example uses the addBehavior method to dynamically attach a behavior that implements a mouseover highlighting effect to all li elements on a page. <script language="jscript">
var collBehaviorID = new Array();
var collLI = new Array ();
var countLI = 0;
function attachBehavior()
{
collLI = document.all.tags ("LI");
countLI = collLI.length;
for (i=0; i < countLI; i++)
{
var iID = collLI[i].addBehavior ("hilite.htc");
if (iID)
collBehaviorID[i] = iID;
}
}
</SCRIPT>
:
//Click <A href="javascript:attachbehavior()">here</A>
//to add a highlighting effect as you hover over each item below.
: This feature requires Microsoft® Internet Explorer 5 or later. Click the following icon to install the latest version. Then reload this page to view the sample.
是否符合公共标准
There is no public standard that applies to this method.
Applies To |
A,
ACRONYM,
ADDRESS,
APPLET,
AREA,
B,
BASE,
BASEFONT,
BGSOUND,
BIG,
BLOCKQUOTE,
BODY,
BR,
BUTTON,
CAPTION,
CENTER,
CITE,
CODE,
COL,
COLGROUP,
COMMENT,
CUSTOM,
DD,
DEL,
DFN,
DIR,
DIV,
DL,
DT,
EM,
EMBED,
FIELDSET,
FONT,
FORM,
FRAME,
FRAMESET,
HEAD,
hn,
HR,
HTML,
I,
IFRAME,
IMG,
INPUT type=button,
INPUT type=checkbox,
INPUT type=file,
INPUT type=hidden,
INPUT type=image,
INPUT type=password,
INPUT type=radio,
INPUT type=reset,
INPUT type=submit,
INPUT type=text,
INS,
ISINDEX,
KBD,
LABEL,
LEGEND,
LI,
LINK,
LISTING,
MAP,
MARQUEE,
MENU,
nextID,
NOBR,
NOFRAMES,
NOSCRIPT,
OBJECT,
OL,
OPTION,
P,
PLAINTEXT,
PRE,
Q,
RT,
RUBY,
S,
SAMP,
SCRIPT,
SELECT,
SMALL,
SPAN,
STRIKE,
STRONG,
STYLE,
SUB,
SUP,
TABLE,
TBODY,
TD,
TEXTAREA,
TFOOT,
TH,
THEAD,
TITLE,
TR,
TT,
U,
UL,
VAR,
WBR,
XML,
XMP | |
更多语法参考 removebehavior, introduction to dhtml behaviors, using dhtml behaviors, about element behaviors, Using HTML Components to Implement DHTML Behaviors in Script
|