一个针对jquery属性选择器的小例子,增加对jquery属性选择器的理解:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript"> <br>$(document).ready(function(){ <br>var subject = ""; <br>var describe = ""; <br><br>//name|="value" <br>$("#attri1").bind("click",function(){ <br>var topValue=$("#attri1").offset().top; <br>subject = "Attribute Contains Prefix Selector [name|=\"value\"]"; <br>describe = "Selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-)."; <br>$("a[hreflang|='en']").css("border","3px dotted green"); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name*="value" <br>$("#attri2").bind("click",function(){ <br>var topValue=$("#attri2").offset().top; <br>subject = "Attribute Contains Selector [name*=\"value\"]"; <br>describe = "Selects elements that have the specified attribute with a value containing the a given substring."; <br>$( "input[name*='man']" ).val( "has man in it!" ); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name~="value" <br>$("#attri3").bind("click",function(){ <br>var topValue=$("#attri3").offset().top; <br>subject = "Attribute Contains Word Selector [name~=\"value\"]"; <br>describe = "Selects elements that have the specified attribute with a value containing a given word, delimited by spaces."; <br>$( "input[name~='man']" ).val( "mr. man is in it!" ); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name$="value" <br>$("#attri4").bind("click",function(){ <br>var topValue=$("#attri4").offset().top; <br>subject = "Attribute Ends With Selector [name$=\"value\"]"; <br>describe = "Selects elements that have the specified attribute with a value ending exactly with a given string. The comparison is case sensitive."; <br>$( "input[name$='letter']" ).val( "a letter" ); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name="value" <br>$("#attri5").bind("click",function(){ <br>var topValue=$("#attri5").offset().top; <br>subject = "Attribute Equals Selector [name=\"value\"]"; <br>describe = "Selects elements that have the specified attribute with a value exactly equal to a certain value."; <br>$( "input[value='Hot Fuzz']" ).next().text( "Hot Fuzz" ); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name$="value" <br>$("#attri6").bind("click",function(){ <br>var topValue=$("#attri6").offset().top; <br>subject = "Attribute Not Equal Selector [name!=\"value\"]"; <br>describe = "Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value."; <br>$( "input[name!='newsletter']" ).next().append( "<b>; not newsletter" ); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name$="value" <br>$("#attri7").bind("click",function(){ <br>var topValue=$("#attri7").offset().top; <br>subject = "Attribute Starts With Selector [name^=\"value\"]"; <br>describe = "Selects elements that have the specified attribute with a value beginning exactly with a given string."; <br>$( "input[name^='news']" ).val( "news here!" ); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name$="value" <br>$("#attri8").bind("click",function(){ <br>var topValue=$("#attri8").offset().top; <br>subject = "Has Attribute Selector [name]"; <br>describe = "Selects elements that have the specified attribute, with any value.<br><b><font color=\"red\">you can click the div which have id element"; <br>$( "div[id]" ).one( "click", function() { <br>var idString = $( this ).text() + " = " + $( this ).attr( "id" ); <br>$( this ).text( idString ); <br>}); <br>showMessage(subject,describe,topValue); <br>}); <br><br>//name$="value" <br>$("#attri9").bind("click",function(){ <br>var topValue=$("#attri9").offset().top; <br>subject = "Multiple Attribute Selector [name=\"value\"][name2=\"value2\"]"; <br>describe = "Matches elements that match all of the specified attribute filters."; <br>$( "input[id][name$='man']" ).val( "only this one" ); <br>showMessage(subject,describe,topValue); <br>}); <br><br><br>}); <br><br>function showMessage(subject,describe,topValue){ <br>$("#showMessage").html("<font color=\"red\"><b>"+subject+"<br>"+describe) <br>.addClass("showMessage").css("margin-top",topValue).hide().show(1000); <br>} <br><br></script>










