Selecting elements in jQuery in Java
Selecting elements in jQuery Generating PDF 417 In Java Using Barcode maker for Java Control to generate, create PDF417 image in Java applications. If you want a JavaScript program to interact with part of your Web page, you have to somehow select the element from the DOM (Document Object Model) hierarchy The most common way to do this in plain JavaScript is with code like this: Print Barcode In Java Using Barcode creation for Java Control to generate, create bar code image in Java applications. var myThing = documentgetElementById( elementId ); Decode Bar Code In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. jQuery allows you to do the same thing in a much shorter syntax
Making PDF417 In C# Using Barcode creation for VS .NET Control to generate, create PDF 417 image in Visual Studio .NET applications. var myThing = $( #elementId ); PDF 417 Creator In .NET Framework Using Barcode drawer for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications. Here s how this line works: Print PDF 417 In Visual Basic .NET Using Barcode encoder for .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications. 1 Create a JavaScript variable
Barcode Printer In Java Using Barcode maker for Java Control to generate, create barcode image in Java applications. You can place a jQuery object in an ordinary JavaScript variable
Paint Data Matrix In Java Using Barcode generation for Java Control to generate, create Data Matrix image in Java applications. 2 The dollar sign indicates a jQuery object
Bar Code Generator In Java Using Barcode maker for Java Control to generate, create bar code image in Java applications. Use the dollar sign symbol followed by a pair of parentheses to indicate you are constructing a jQuery object Print Code 128 In Java Using Barcode generation for Java Control to generate, create Code 128 Code Set B image in Java applications. 3 Use a CSS-style selector to indicate which object you are referring to
Drawing Barcode In Java Using Barcode generation for Java Control to generate, create barcode image in Java applications. Putting jQuery to Work
Make USPS OneCode Solution Barcode In Java Using Barcode generator for Java Control to generate, create OneCode image in Java applications. In CSS syntax, you can use #myThing to indicate an element with the myThing ID You can also use p to indicate all paragraphs, or myClass to indicate all elements with the myClass class defined Bar Code Reader In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. 4 The resulting object can do more than a regular DOM object
Read EAN-13 In .NET Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications. Because the $ function creates a special jQuery object, your resulting variable can do anything jQuery objects can do The jQuery code is shorter and easier to read, is more flexible, and creates a more powerful object than the JavaScript original Code 128 Encoder In VS .NET Using Barcode generator for Visual Studio .NET Control to generate, create Code 128 Code Set B image in .NET applications. Selecting all elements of a specific type
Painting UPC Code In .NET Using Barcode generator for .NET framework Control to generate, create UPC-A Supplement 5 image in .NET framework applications. You may find that you want to do something interesting with all the elements of a particular type of tag As an example, look at the modListhtml program in Figure 2-4 Although there is a border element around the list, the page has no CSS! The border was added dynamically through jQuery There s another surprise: Click on any list item, and the contents of that item appear in a dialog box, as shown in Figure 2-5 Code-128 Encoder In C# Using Barcode generation for VS .NET Control to generate, create Code 128 Code Set A image in VS .NET applications. Book VII 2
Printing EAN / UCC - 13 In .NET Framework Using Barcode generator for .NET framework Control to generate, create UPC - 13 image in .NET applications. Improving JavaScript with jQuery
Encode Barcode In VB.NET Using Barcode encoder for Visual Studio .NET Control to generate, create bar code image in .NET applications. Figure 2-4: Each element of this list has a border
EAN-13 Printer In Visual C# Using Barcode maker for .NET framework Control to generate, create GTIN - 13 image in VS .NET applications. Putting jQuery to Work
Figure 2-5: The list items all now have a click() method
These features can be added in plain JavaScript, but they would be tedious jQuery makes it very easy to manipulate all the elements of a particular type Here s the code for modListhtml: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 10 Strict//EN http://wwww3org/TR/xhtml1/DTD/xhtml1-strictdtd > <html lang= EN dir= ltr xmlns= http://wwww3org/1999/xhtml > <head> <meta http-equiv= content-type content= text/xml; charset=utf-8 /> <title>modListhtml</title> <script type = text/javascript src = jquery-123minjs ></script> <script type = text/javascript > //<![CDATA[ $(document)ready(modifyListItems); function modifyListItems(){ var items = $( li ); itemscss( border , 1px red solid ); itemsclick(sayValue); } // end modifyListItems function sayValue(){ alert($(this)html()); } // end sayValue //]]> Putting jQuery to Work
</script> </head> <body> <h1>Show items</h1> <ul> <li>uno</li> <li>dos</li> <li>tres</li> <li>quatro</li> <li>sinco</li> </ul> </body> </html> The overall structure isn t too hard to see: There is no CSS style No internal or external CSS is defined for this page Any CSS is generated dynamically by the JavaScript/jQuery code The XHTML document contains a list For this example, I add some functionality to all the li elements on the page, but I could just as easily add functionality to any other element all p tags or headers, for example The jQuery library is included The jQuery library adds the functionality required for this project The modifyListItems() method is called when the document is ready Like most jQuery programs, much of the action happens once the page is ready for processing In this case, I call the modifyListItems() method as soon as the DOM object is ready (See the upcoming section, Modifying the list items, for details on how to write this function) The sayValue() method will be used to indicate the text associated with a specific element This function is used to output the value of an element Its use is explained in the next section Book VII 2
Improving JavaScript with jQuery
Modifying the list items
The main purpose of the jQuery code in this page is to illustrate how to change the appearance and behavior of all instances of a particular element (in this case, all li elements) function modifyListItems(){ var items = $( li ); itemscss( border , 1px red solid ); itemsclick(sayValue); } // end modifyListItems function sayValue(){ alert($(this)html()); } // end sayValue Putting jQuery to Work
Here s how it works: 1 Create a jQuery object called items
Use the $( li ) selector to refer to every li element on the page All li items will now be encased in a special variable called items var items = $( li );
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |