The specific code is as follows:
<script type="text/javascript" src="js/jquery-1.8."></script> <script type="text/javascript"> $(document).ready(function(){ $("#btn").click(function(){ //Use attr(name) to get the attribute value: alert($("p").attr("title"));//Your favorite fruit is. alert($("ul li:eq(1)").attr("title"));//Orange juice alert($("ul li:eq(1)").attr("alt"));//123 //Use attr(name, value) to change the attribute value: //$("ul li:eq(1)").attr("title", "or?");//Change the value of the title of the second li //Use attr(name, fn) to set the function value of the attribute $("ul li:eq(1)").attr("title", function(){ return "I don't believe it"; });//Change the value of the title of the second li alert($("ul li:eq(1)").attr("title"));//I don't believe it //Use attr(name, properties) to set an object in the form of "name/value" to the attributes of all matching elements $("ul li:eq(1)").attr({title: "First", alt: "Second"}); alert($("ul li:eq(1)").attr("title"));//The first alert($("ul li:eq(1)").attr("alt"));//The second }); }); </script> <p title="Your favorite fruit is.">Your favorite fruit is?</p> <ul> <li title="Apple juice">apple</li> <li title="Orange Juice" name="tasty" alt="123">tangerine</li> <li title="Pineapple juice">Pineapple</li> </ul> <input type="button" value="Click" />
Summarize
The above is a detailed explanation of the usage of Attr in JS introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!