jQuery - DOM属性関連操作
jQueryを通してDOMの属性の取得、追加、削除の操作が可能。
取得
- attr(name)
- prop(propName)
追加
- attr(name, value)
- attr(map)
attr(key, fn)
prop(properties)
- prop(name, value)
- prop(key, fn)
削除
- removeAttr(name)
- removeProp(name)
※attrとpropの違いは・・・・
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title> DOMの属性操作 </title> </head> <body> <img/><img/> <div> <img/><img/><img/> </div> <script type="text/javascript" src="jquery-1.12.0.min.js"> </script> <script type="text/javascript"> // 属性追加 $("body>img").attr("src" , "logo.jpg") .attr("alt" , "logo"); // fnにより属性追加 $("div>img").attr("src" , function(index) { return index + 1 + ".png"; }); </script> </body> </html>