XUL Tutorial: Defining Methods for XBL elements

Source: Internet
Author: User
Tags anonymous

Original title: XUL tutorial-bindings-adding Methods

Original Author: Neil Deakin

Original address: http://www.xulplanet.com/tutorials/xultu/xblmethods.html

The following is the translation of the original text:

Next we'll learn how to add methods to the elements defined in XBL.

Method (Methods)

Since you can add attributes to the elements defined by XBL, you can naturally add methods that can be invoked in scripts. The method behaves as a function of an object, such as "window.open ()". You can add custom methods to your own elements by using the method element. The syntax for adding methods is described below:

<implementation>
<method name="method-name">
<parameter name="parameter-name1"/>
<parameter name="parameter-name2"/>
.
.
.
<body>
-- 在这里编写方法的脚本代码 --
</body>
</method>
</implementation>

Methods are also defined within the implementation element, just as fields and properties are defined. The method element contains two types of child elements: The parameter element is used to describe the parameters of the methods, and the BODY element is used to contain the script's code.

The value of the name attribute corresponds to the name of the method. Similarly, the name attribute on the parameter element corresponds to the names of each parameter. Each parameter element corresponds to a parameter of a method. Specifically, if the method has 3 parameters, then there will be 3 parameter elements that correspond. If the method itself has no parameters, then you do not have to define any parameter elements.

The BODY element contains the script to execute when the method is called. The name of the parameter corresponds to the variable in the script, as if it were passed from a parameter. For example, putting the following JavaScript function in the XBL may be the next:

function getMaximum(num1,num2)
{
if (num1<=num2) return num2;
else return num1;
}
XBL:
<method name="getMaximum">
<parameter name="num1"/>
<parameter name="num2"/>
<body>
if (num1<=num2) return num2;
else return num1;
</body>
<method>

This function, called getmaximum, can be used to compare the size of two numbers, and the number to compare is passed to the method by argument. Note that the less-than symbol is replaced, otherwise it will be confused with the label's starting symbol. You can also include the entire piece of code in a CDATA block. You can invoke this method by using code such as "Element.getmaximum" (5,10), which is a reference to the XBL element that contains the Getmaximum method.

You can use the parameter tag to define parameters for a method. Because Mozilla uses JavaScript as its own scripting language, and JavaScript is a weakly typed language, you don't have to define a type for a parameter. In the future, of course, other languages may also be used in conjunction with XBL.

accessing anonymous content (accessing the Anonymous contents)

You must have experienced the need to modify some elements of the anonymous content, but since these elements were established anonymously, it is not possible to use regular access to the DOM. Because developers don't need to know how they do it, they can be hidden from developers as long as they know what they do. But there are really special Russian methods that can manipulate elements in anonymous content.

Elements that use the XBL description behavior have a special array property that holds all of the anonymous child elements. Each item in the array corresponds to a child element under the XBL element. This property cannot be accessed directly, and must be accessed indirectly using the Getanonymousnodes method of document:

var value=document.getAnonymousNodes(element);

here, element; must be set to a reference to the element that contains the anonymous content you want to access, which returns an array of elements, corresponding to the content of Yu in anonymous. If you want to use which element, you can use the DOM directly, because the elements of the developer are now visible. Note that there are XBL element nesting situations where you may need to use the Getanonymousnodes function two times.

The following example creates a single line of buttons:

<binding id="buttonrow">
<content>
<button label="Yes"/>
<button label="No"/>
<button label="Sort Of"/>
</content>
</binding>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.