<!DOCTYPE html> <html> <head> <style> img { padding:10px; } div { color:red; font-size:24px; } </style> <script src="http://code.jquery.com/jquery-1.5.js"></script> </head> <body> <img /> <img /> <img /> <div><B>Attribute of Ajax</B></div> <script> $("img").attr({ src: "/images/hat.gif", title: "jQuery", alt: "jQuery Logo" }); $("div").text($("img").attr("alt")); </script> </body> </html>
Demo:
Example: Disable buttons greater than the 1st button.
<!DOCTYPE html> <html> <head> <style> button { margin:10px; } </style> <script src="http://code.jquery.com/jquery-1.5.js"></script> </head> <body> <button>0th Button</button> <button>1st Button</button> <button>2nd Button</button> <script> $("button:gt(1)").attr("disabled","disabled"); </script> </body> </html>
Demo:
Example: Set the id for divs based on the position in the page.
<!DOCTYPE html> <html> <head> <style> div { color:blue; } span { color:red; } b { font-weight:bolder; } </style> <script src="http://code.jquery.com/jquery-1.5.js"></script> </head> <body> <div>Zero-th <span></span></div> <div>First <span></span></div> <div>Second <span></span></div> <script> $("div").attr("id", function (arr) { return "div-id" + arr; }) .each(function () { $("span", this).html("(ID = '<b>" + this.id + "</b>')"); }); </script> </body> </html>
Demo:
Example: Set the src attribute from title attribute on the image.
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.5.js"></script> </head> <body> <img title="hat.gif"/> <script> $("img").attr("src", function() { return "/images/" + this.title; }); </script> </body> </html>
Demo:
|