The preceding section describes how to use the $ function to filter elements on the page and replace onload events. $ Function also has a function. When its parameter is an HTML segment, it returns a package set containing this HTML element. For example:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> jQuery Wrapper-Liehuo.net </title>
<Script type = "text/javascript" src = "jquery-1.3.2.js"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ("<P> I am a red line. </p>" Colors .css ("color", "Red"). appendTo ("# foo ");
});
</Script>
</Head>
<Body>
<Div id = "foo">
</Div>
</Body>
</Html>
$ Creates a packaging element for the p element, which is the same as the selected element with the aforementioned selector. You can also call some jQuery methods, and finally use appendTo (described later) to move this element to the div. The final result is:
The following describes the functions used to operate the package set.
1. html (), html (text). The first function returns the package set.FirstThe html in the element. The second function will encapsulateAllThe internal html of the element is set to text.
2. size (). Returns the elements contained in the package.
3. get (), get (n ). The first function returns the HTML elements in the encapsulated set in the form of a javascript array, and the second returns the nth HTML element.
4. index (elem). Return the location of the incoming HTML element elem in the package set. -1 is returned if it is not in the packaging set.
5. The add (s), add (elem), and add (array). add functions are used to add elements to the packaging set. If it is a selector, add all selected elements. If it is an HTML clip, create an HTML element and add it. If it is an array of HTML elements or HTML elements, then, add it directly.
6. not (expression), filter (expression). These two functions are used to filter elements of the package set. Expression can be a string (selector), an html element, or an array of elements. The not function removes the elements that match the selector or that are contained in the array. Filter, on the contrary, leaves elements that match the selector or elements contained in the array.
7. The slice (begin, end) function returns a new package set whose content is the elements from the begin to the end. If end is omitted, it indicates the maximum length.
Let's look at several examples.
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> jQuery Wrapper-Liehuo.net </title>
<Script type = "text/javascript" src = "jquery-1.3.2.js"> </script>
<Script type = "text/javascript">
$ (Function (){
Certificate ('span'0000.html ($ ('lil'). size ());
$ ('Lil'). not ('li: first '). add ('P: first'example .css ('color', 'red ');
$ ('Lil'). get (0). innerHTML = "First Special Item ";
$ ('P: last'0000.html(('li'0000.html ());
});
</Script>
</Head>
<Body>
<Ul>
<Li> First Item </li>
<Li> Second Item </li>
<Li> Third Item </li>
<Li> Forth Item </li>
<Li> th Item </li>
</Ul>
<P> I am a lonely p. </p>
<P> The UL has <span> </span> elements. </p>
<Span> Hello jQuery. </span>
<P> </p>
</Body>
</Html>
- Three pages in total:
- Previous Page
- 1
- 2
- 3
- Next Page