1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>第3章示例5</title> 6 <style type="text/css"> 7 body { width:780px; } 8 div,p,h3,span { border:4px solid black; background-color:green; color:white; float:left; margin:6px; padding:5px; font:bold 14px/1 Arial,Helvetica,sans-serif; width:220px; display:block; } 9 div p,div span { width:205px; border-width:2px; margin:5px 0; float:none; } 10 p span { width:193px; border-width:1px;} 11 h3 { margin-right:500px; } 12 .clear { clear:both; } 13 .relative { position:relative; } 14 .highlight { background-color:gold; color:black; } 15 form { clear:both; font:bold 14px/1 Arial,Helvetica,sans-serif; } 16 button { font:bold 16px/1 Arial,Helvetica,sans-serif; margin:1px 3px; padding:2px; cursor:pointer; } 17 </style> 18 <script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script> 19 <script type="text/javascript"> 20 $(document).ready(function(){ 21 $("button").click(function(){$("*").removeClass("highlight");}); 22 $("#btn1").click(function(){$("div").add("p:last").addClass("highlight");}); 23 $("#btn2").click(function(){$("body").children(":eq(3)").prevAll("[class]").andSelf().addClass("highlight");}); 24 $("#btn3").click(function(){$("body").children(":eq(3)").prevAll("[class]").andSelf().andSelf().addClass("highlight");}); 25 $("#btn4").click(function(){$("body").children(":eq(3)").prevAll("[class]").andSelf().children("p").andSelf().addClass("highlight");}); 26 }); 27 </script> 28 </head> 29 <body> 30 <div class="clear"> 31 <div class="clear"> 32 <p> 33 <p> 34 <span><span> </span></span> 35 </p> 36 </p> 37 </div> 38 </div> 39 <div> 40 <div> 41 <p><p> </p></p> 42 <p><p> </p></p> 43 <p><p> </p></p> 44 </div> 45 </div> 46 <div class="relative"> 47 <div class="relative"> 48 <span><span> </span></span> 49 <span><span> </span></span> 50 <span class="clear"><span class="clear"> </span></span> 51 </div> 52 </div> 53 <h3 class="clear"><h3 class="clear"> </h3></h3> 54 <div class="clear"> 55 <div class="clear"> 56 <p> 57 <p> 58 <span><span> </span></span> 59 <span><span> </span></span> 60 </p> 61 </p> 62 <p> 63 <p> 64 <span><span> </span></span> 65 <span><span> </span></span> 66 </p> 67 </p> 68 </div> 69 </div> 70 <div> 71 <div> 72 <p><p> </p></p> 73 <p><p> </p></p> 74 <p><p> </p></p> 75 </div> 76 </div> 77 <div class="relative"> 78 <div class="relative"> 79 <span><span> </span></span> 80 <span><span> </span></span> 81 <span class="clear"><span class="clear"> </span></span> 82 </div> 83 </div> 84 <form> 85 <button type="button" id="btn1">$("div").add("p:last")</button> 86 <button type="button" id="btn2">$("body").children(":eq(3)").prevAll("[class]").andSelf()</button> 87 <button type="button" id="btn3">$("body").children(":eq(3)").prevAll("[class]").andSelf().andSelf()</button> 88 <button type="button" id="btn4">$("body").children(":eq(3)").prevAll("[class]").andSelf().children("p").andSelf()</button> 89 </form> 90 </body> 91 </html>