Before the release of Microsoft IE 5.0 browser, the biggest challenge in Web programming is that you cannot easily create components to achieveCodeReuse and multi-page sharing. This problem has plagued DHTML (Dynamic heml) web programmers. They can only repeatedly write HTML, CSS, and JavaScript code to meet repeated or similar features on multiple pages. This situation has been improved since the release of the IE 5.0 browser. It brings us a new command combination method that can encapsulate code that implements specific functions in one component, in this way, code can be reused on multiple pages, allowing Web programming to enter a new world. This new technology is the behaviors in DHTML ).
As an easy-to-use component, "behavior" encapsulates specific functions or actions on the page. When a "behavior" is attached to a component on the web page, the original behavior of the component changes. Therefore, web programmers can develop common DHTML commands, modify some attributes of the original object, use "behavior" to enhance the function of an object, and simplify the HTML code of the page. In addition, the creation and use of "behavior" is very simple and convenient. The required knowledge is just the CSS style sheet, HTML instructions, and JavaScript scripting language that we used to use. As long as you have some knowledge about this, have had practical programming experience, and learn and master the use of "behavior", there is no problem at all. We will take a "behavior" component that changes the font effect as an example to illustrate how to write and use a "behavior" and experience the advantages and convenience of "behavior" for page editing.
First, create a text file named font_effect.htc. All the files that make up the "behavior" component use the. HTC extension. The content in this file is our description of this "behavior. The steps for creating and using it are as follows:
(1) first, add several event responses to this "action". The statement format is as follows:
<Public: attach event = "onmouseover" onevent = "glowit ()"/>
<Public: attach event = "onmouseout" onevent = "noglow ()"/>
<Public: attach event = "onmousedown" onevent = "font2yellow ()"/>
<Public: attach event = "onmouseup" onevent = "font2blue ()"/>
"Event" corresponds to the required event names, which are onmouseover, onmouseout, onmousedown, and onmouseup respectively. You can add other event names to meet your specific needs. Onevent corresponds to an event handle, that is, the name of the function called when the event is triggered. The glowit () function generates a red glow around the font. The noglow () function eliminates the font glow. The font2yellow () function changes the font color to yellow. The font2blue () function changes the font color to blue. The definitions of the four events are similar.
(2) Next, add two "methods" for this "behavior". The content is as follows.
<Public: method name = "move_down"/>
<Public: method name = "move_right"/>
The "name" parameter corresponds to the given "method" name. Move_down and move_right are the function names corresponding to the "method" that moves down and right respectively. Note: do not include "()" parentheses after the method name, that is, do not write "move_down ()". This is not allowed in the syntax defined by "method.
(3) The next step is to compile the function content corresponding to the "event handle" and "method" using Javascript script statements in the familiar DHTML environment to achieve the expected results. For details, refer to the following source Program . The "element" parameter indicates the object to which the "behavior" is attached, because the "behavior" is always attached to the page element and plays a role through this element. Other statements are the programming content of DHTML. If you have any questions, refer to the IE browser content in Microsoft's msdn development documentation, which contains detailed DHTML programming reference content, attributes, and method instructions, and a large number Article And example programs. I often visit Microsoft's msdn documentation, especially for beginners. It is a good learning habit. You can get almost any answers you want. Its website is http://msdn.microsoft.com/ie /.
The complete "behavior" document "font_0000t.htc" contains the following content:
/// // The "action" document starts /////////// /////////////////
// Add four mouse events to "behavior"
<Public: attach event = "onmouseover" onevent = "glowit ()"/>
<Public: attach event = "onmouseout" onevent = "noglow ()"/>
<Public: attach event = "onmousedown" onevent = "font2yellow ()"/>
<Public: attach event = "onmouseup" onevent = "font2blue ()"/>
// Define two methods for "behavior"
<Public: method name = "move_down"/>
<Public: method name = "move_right"/>
<Script language = "jscript">
// Define a variable for saving the font color
VaR font_color;
// Define the method for moving down text
Function move_down ()
{
Element. style. postop + = 2;
}
// Define the method for moving text to the right
Function move_right ()
{
Element. style. posleft + = 6;
}
// Define the call function of the mouse onmouseup event
Function font2blue (){
If (event. srcelement = element)
{
Element. style. Color = 'Blue ';
}
}
// Define the call function of the mouse onmousedown event
Function font2yellow (){
If (event. srcelement = element)
{
Element. style. Color = 'yellow ';
}
}
// Define the call function of the mouse onmouseover event
Function glowit ()
{
If (event. srcelement = element)
{
Font_color = style. color;
Element. style. Color = 'white ';
Element. style. Filter = "Glow (color = Red, strength = 2 )";
}
}
// Define the call function of the mouse onmouseout event
Function noglow ()
{
If (event. srcelement = element)
{
Element. style. Filter = "";
Element. style. Color = font_color;
}
}
</SCRIPT>
///// // The End of the "action" document ///////////////// //////////////
(4) how to use "behavior" on a page"
You do not need to learn new knowledge when using the "behavior" component on the page. The required knowledge is only about CSS style sheets and HTML settings. See the following statements.
<Style>
. Myfilter {behavior: URL (font_0000t.htc); position: relative; font-weight: bold; width = 180; left: 0 ;}
</Style>
We can see that this is exactly the same as the style sheet settings we have known before. The preceding statement defines a style name: "myfilter". For us, the new content is: "behavior: URL (font_0000t.htc );", "behavior" is the new "behavior" attribute name, which is the setting method of "behavior" in the style sheet. The content in the brackets is the file name of the "behavior" document. In this example, it indicates that the "behavior" document is in the same directory as the page file. If the "behavior" document is placed in another directory, add the corresponding path name before this parameter to ensure that the location of the "behavior" document can be correctly located. The other content in this "style" is the general style attribute settings, which can be increased or decreased according to your needs. However, in this example, because the "Glow" filter effect is used, you must set at least one width attribute. By specifying the style above, we have a style named "myfilter" with a "behavior" with a font change effect ". If you want to use the style with "behavior" on a page element, it is also very easy to place this "style name" in the attribute setting area of the component, see the following statement.
<Span id = "myspan" class = 'myfilter'> text effects produced by behaviors </span> <br>
<SPAN class = 'myfilter'> A glow is generated when the cursor points to it. </span>
There is no new content in the above statement, and class = 'myfilter' is the style setting we are familiar. A "ID" mark is also defined in the attribute of the first "span" mark. It will be seen later. This is used to demonstrate the setting of calling the "Method" in "behavior. After this setting, the content in the "span" component shows the predefined effect in the "behavior" component:
1. When the mouse pointer moves to the text content, a red glow appears around the text, and the text turns white.
2. When the mouse button is pressed, the text color changes to yellow.
3. After the mouse button is raised, the text color changes to blue.
4. When the mouse pointer moves beyond the text area, the red glow is removed and the text is restored to its original state.
In addition, we set two "methods", "move_down" and "move_right" when defining "behavior ". To call these two "methods", two buttons are defined:
<Button onclick = "myspan. move_right ();"> move the first line of text to the right </button>
<Br>
<Button onclick = "myspan. move_down ();"> move the first line of text down </button>
Use the onclick event of the button to call the two "methods". The previously defined "ID" is used as the object name of the component, and the "myspan. move_down "to call the" method "and manipulate this object. As you can see, after you press the corresponding button, the text in the first line will be moved down or to the right. Although it only demonstrates the use of the first line of text, you can also move other objects as long as the corresponding settings are made. The complete content of the page source document is as follows:
<HTML>
<Head>
<Title> behavior effect demonstration </title>
<Style>
. Myfilter {behavior: URL (font_0000t.htc); position: relative; font-weight: bold; width = 180; left: 0 ;}
</Style>
</Head>
<Body>
<Span id = "myspan" class = 'myfilter'> text effects produced by behaviors </span> <br>
<SPAN class = 'myfilter'> A glow is generated when you point to it. </span> <br>
<SPAN class = 'myfilter'> White text at the same time </span> <br>
<SPAN class = 'myfilter'> press the mouse and the text turns yellow </span> <br>
<SPAN class = 'myfilter'> move the mouse up and the text turns blue </span> <br>
<SPAN class = 'myfilter'> the text is restored after the mouse leaves </span> <br>
<Button onclick = "myspan. move_right ();"> move the first line of text to the right </button> <br>
<Button onclick = "myspan. move_down ();"> move the first line of text down </button>
</Body>
</Html>
Through the simple introduction above, we can see that we can easily combine a variety of text changes in a "behavior" at the same time, through simple "style" settings, connecting it to page components arbitrarily reflects the advantages and powerful functions of the "behavior" component. A "behavior" component can be used not only on one page, but also on all pages on the same site. If you do not use "behavior" to achieve the above effect, you can call a set of predefined functions on the page to complete the same function, however, each component on the page that uses text effects must be appended with four mouse events. If the same effect is used on multiple pages, the called function must be set repeatedly on each page. In contrast, the advantages and disadvantages are obvious. Therefore, you can use the "behavior" component to create a simple, efficient, universal, and easy-to-maintain page. The example in this article is only to illustrate the compilation and use process of the "behavior" component, so that readers can have a general understanding of "behavior" programming, based on these components, you can create the "behavior" components you need, or directly reference the ready-made "behavior" components that meet your individual needs, because the concept of "component sharing" is also the original intention of "behavior" developers. Finally, I hope this article will serve as a "Reference" to bring readers into the wonderful DHTML web page programming world.
Note:
HTC is the abbreviation of HTML component,
Is one of the major extensions of ie5.0,
In addition to the reusable advantages of general components,
It also has advantages such as ease of development and use,
Because external files need to be introduced, we will not give an example here, but there are examples in the treasure.
Controls and components
HTC provides a simple mechanism to implement DHTML behavior in scripts. There is no difference between an HTC file and an HTML file, and it is suffixed with ". HTC,
You can use HTC to perform the following actions:
Set attributes and methods. Defined by "property" and "method" Elements
Set custom events. Use the "event" element to release the event using the "Fire ()" method of the element,
Use the createeventobject () method to set the event environment.
Access the DHTML Object Model of the HTML page containing the HTC, and use the HTC "element" object to return
An element of an additional action. With this object, HTC can access the Object Model (attributes, methods, and events) that contains the document ).
Receive notifications using the "Attach" element. The browser not only notifies HTC of standard DHTML events, but also two special events: oncontentready events and ondocumentready events.
Define tags and namespaces
HTC is based on custom tags
To customize a tag for a page, you must provide a namespace for the tag
To use this tag, you must add the correct XML namespace prefix before the tag.
For example:
Example of defining a new right mark
The code snippet is as follows:
<HTML xmlns: docjs>
<Head>
<Style>
@ Media all {
Docjs \: right {text-align: Right; width: 100}
}
</Style>
</Head>
<Body>
<Docjs: Right>
Read Doc JavaScript's columns, tips, tools, and tutorials
</Docjs: Right>
</Body>
</Html>
You can define multiple namespaces in a single HTML Tag:
<HTML xmlns: docjs xmlns: docjavascript>
Component Definition
The component name is determined by the XML namespace defined in the first line in the HTC document.
This page has only one namespace definition without calling other HTC words.
In fact, the definition of HTML components is the definition of custom tag behavior.
This action includes an attribute and an event:
<HTML xmlns: mytag>
<Head>
<Public: component tagname = "mytag">
<Property name = "value"> </property>
<Attach event = "oncontentready" onevent = "fninit ()" <>/Attach>
</Public: component>
<Style> // define a style sheet for the component
. Cssmytag {
}
</Style>
<Script language = JavaScript>
Function mytagbehavior1 () {}// define a method for the component
</SCRIPT>
</Head>
<Body onclick = mytagbehavior1> // define a response event for the component
</Body>
</Html>
Oncontentready is triggered when the component is completely imported by the caller.
Let's take a look at fninit ()
Function fninit (){
Document. Body. innerhtml = element. value; // sets the display content of the component.
Document. Body. classname = "clsmytag"; // sets the display style table,
Defaults. viewlink = Document; // make this component visible to other documents
Element. aproperty = element. value; // you can specify the attribute value of a widget.
}
Call of components
<HTML xmlns: mycom>
<Head>
<? Import namespace = "mycom" implementation = "mytag. HTC"/>
</Head>
<Body>
<Mycom: mytag> </mycom: mytag>
</Body>
</Html>