| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|---|
| 2 | <html>
|
|---|
| 3 | <head>
|
|---|
| 4 | <title>Tester</title>
|
|---|
| 5 | <style type="text/css">
|
|---|
| 6 | #container { background:yellow; width:200px; }
|
|---|
| 7 | </style>
|
|---|
| 8 | <script type="text/javascript" src="jquery.js"></script>
|
|---|
| 9 | <script type="text/javascript">
|
|---|
| 10 | function parseToXML(text) {
|
|---|
| 11 | var doc;
|
|---|
| 12 |
|
|---|
| 13 | if (window.ActiveXObject) {
|
|---|
| 14 | doc = new ActiveXObject("Microsoft.XMLDOM");
|
|---|
| 15 | doc.async = false;
|
|---|
| 16 | doc.loadXML(text);
|
|---|
| 17 | } else
|
|---|
| 18 | doc = (new DOMParser()).parseFromString(text, "application/xml");
|
|---|
| 19 |
|
|---|
| 20 | return doc;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | if (typeof console == 'undefined') {
|
|---|
| 24 | var console = {};
|
|---|
| 25 | console.log = alert;
|
|---|
| 26 | }
|
|---|
| 27 | var xmlText = "<Test><Items><item/><Item/><Item/></Items></Test>";
|
|---|
| 28 |
|
|---|
| 29 | function doHTML() {
|
|---|
| 30 | var $n = $(xmlText);
|
|---|
| 31 | console.log($("item", $n).length); // expected 1, found 1
|
|---|
| 32 | console.log($("Item", $n).length); // expected 2, found 1
|
|---|
| 33 | console.log($n.find("Items").children("Item").length); // expected 2, found 0
|
|---|
| 34 | console.log($n.children("Items").children("Item").length); // expected 2, found 3
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | function doXML() {
|
|---|
| 38 | var doc = parseToXML(xmlText);
|
|---|
| 39 | console.log("expect 1 found " + $("item", doc).length);
|
|---|
| 40 | console.log("expect 2 found " + $("Item", doc).length);
|
|---|
| 41 | console.log("expect 1 found " + $("Test", doc).find("Item:first").length);
|
|---|
| 42 | console.log("expect 2 found " + $("Test", doc).find("Items").find("Item").length);
|
|---|
| 43 | console.log("expect 2 found " + $("Items Item", doc).length);
|
|---|
| 44 | // fix this by removing toUpperCase from from jQuery.find()
|
|---|
| 45 | console.log("expect 2 found " + $("Items > Item", doc).length);
|
|---|
| 46 | // fix this by removing toUpperCase from from jQuery.filter()
|
|---|
| 47 | console.log("expect 2 found " + $("Items", doc).children("Item").length);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | $(function () {
|
|---|
| 51 | $("button:first").click(doHTML);
|
|---|
| 52 | $("button:last").click(doXML);
|
|---|
| 53 | });
|
|---|
| 54 | </script>
|
|---|
| 55 | </head>
|
|---|
| 56 | <body>
|
|---|
| 57 | <button>As HTML (in ticket description)</button>
|
|---|
| 58 | <button>As XML (parsed to an XML document)</button>
|
|---|
| 59 | <div id="container">
|
|---|
| 60 | Hi
|
|---|
| 61 | </div>
|
|---|
| 62 | <div id="adiv"></div>
|
|---|
| 63 | </body>
|
|---|
| 64 | </html>
|
|---|