<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>Select Element by ID</title> </head> <body> <p id="hello">Hello World</p> <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script> <script type="text/javascript"> //Select the element by it's ID alert($("#hello").text()); // Hello World </script> </body> </html>
#;&,.+*~':"!^$[]()=>|/
$(“#some:id”) -> $(“#some\\:id”)
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>Select Element by class selector</title> </head> <body> <p id="hello"> <span class="helloworld">Hello</span> <span class="helloworld">World</span> </p> <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script> <script type="text/javascript"> //Select the element by it's classname alert($(".helloworld").text()); // HelloWorld alert($(".helloworld").length); // 2 </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>Select Element by Attribute</title> </head> <body> <form> First Name: <input type="text" name="first_name" value="Ralph" /> <br /><br /> Last Name: <input type="text" name="last_name" value="Whitbeck" /> <br /><br /> </form> <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script> <script type="text/javascript"> //Select the element by attribute alert($("input[name=first_name]").val()); // Ralph </script> </body> </html>
<ul> <li><a href="http://jquerymobile.com">jQuery Mobile</a></li> </ul> <div> <ul> <li><a href="http://jquery.com">jQuery</a></li> <li><a href="http://jqueryui.com">jQuery UI</a></li> </ul> </div>
$("div ul");
<ul> <li><a href="http://jquery.com">jQuery</a></li> <li><a href="http://jqueryui.com">jQuery UI</a></li> </ul>
$("li > a");
<a href="http://jquerymobile.com">jQuery Mobile</a> <a href="http://jquery.com">jQuery</a> <a href="http://jqueryui.com">jQuery UI</a>
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>Select Element by Selector Filter</title> </head> <body> jQuery project sites: <ul> <li><a href="http://jquery.com">jQuery</a></li> <li><a href="http://jqueryui.com">jQuery UI</a></li> <li><a href="http://sizzlejs.com">Sizzle</a></li> <li><a href="http://jquery.org">jQuery Project</a></li> </ul> <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js"></script> <script type="text/javascript"> //Select the element by attribute alert($("a:first").attr("href")); // http://jquery.com </script> </body> </html>
If ($(“div”).length) { //do something. }
$(“div”).show(); $(“p”).show();
$(“div, p”).show();
$(“a[href=http://jquery.com][class=mainnav]”).show();
$(“a:contains(jQuery):first).show();
var $p = $(“p”); $p.show(); $p.hide(); $p.show();