<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> </title> <Style type = "text/css"> Div {width: 100px; margin: 0px auto; border: 1px solid red ;} </Style> <Script src = "jquery-1.6.2.min.js" type = "text/javascript"> </script> <Script type = "text/javascript"> $ (Function (){ $ ("# BtnChange"). click (function (){ Var $ divs = $ ("div "); // ---- 1. Example 1 // $ Divs. eq (0). attr ("MyAttr", "Ronaldo "); // Return the specified JQuery object for the eq (Note: $ divs has two div dom elements) // $ Divs. eq (1). attr ("MyAttr", "Ronaldo1 "); // Alert ($ divs. attr ("MyAttr ")); // Read the MyAttr attribute. Note that only the attribute of the first element is read. // ---- 2. Example 2 setting attr in JSon format // $ Divs. eq (0). attr ({"MyAttr": "Ronaldo", "Age": "19 "}); // $ Divs. eq (1). attr ({"MyAttr": "Rivaldo", "Age": "29 "}); // Alert ("MyAttr:" + $ divs. eq (1 ). attr ("MyAttr") + ", Age:" + $ divs. eq (1 ). attr ("Age ")); // ---- 3. Example 3 remove an attribute $ Divs. eq (0). attr ({"MyAttr": "Ronaldo", "Age": "19 "}); $ Divs. eq (1). attr ({"MyAttr": "Rivaldo", "Age": "29 "}); Alert ("MyAttr:" + $ divs. eq (1 ). attr ("MyAttr") + ", Age:" + $ divs. eq (1 ). attr ("Age ")); $ Divs. eq (1). removeAttr ("Age "); Alert ("MyAttr:" + $ divs. eq (1 ). attr ("MyAttr") + ", Age:" + $ divs. eq (1 ). attr ("Age ")); }); }); </Script> </Head> <Body> <Input id = "btnChange" type = "button" value = "Click Me"/> <Div id = "div1"> I am div1 </div> <Div id = "div2"> I am div2 </div> </Body> </Html> |