<!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function () { $("button").click(function () { alert($("p").attr("title"));//获取属性 // this code can only get the first element‘s attribute. }); }); </script> </head> <body> <p title="title1">paragraph 1</p> <p title="title2">paragraph 2</p> <br/> <button>get title</button> </body> </html>
<script type="text/javascript"> $(document).ready(function () { $("button").click(function () { //get attribute for every element in selection. $("p").each(function () { alert($(this).attr("title")); }); }); }); </script>
即可分别获取每个元素的属性.
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#button1").click(function(){ $("p").attr("title","Hello World"); }); }); </script> </head> <body> <input type="button" id="button1" value="button1"></input> <p>This is a paragraph.</p> <div>This is a div.</div> <p>This is another paragraph.</p> <div>This is another div.</div> </body> </html>
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#button1").click(function () { $("p").attr("title", "Hello World"); }); $("#button2").click(function () { $("div").attr({"title": "some title for div", "hello": "World"}); }); }); </script> </head> <body> <input type="button" id="button1" value="button1"></input> <input type="button" id="button2" value="button2"></input> <p>This is a paragraph.</p> <div>This is a div.</div> <p>This is another paragraph.</p> <div>This is another div.</div> </body> </html>
<!DOCTYPE html> <html> <head> <style> div { color: blue; } span { color: red; } b { font-weight: bolder; } </style> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function () { $("div") .attr("id", function (index, oldAttr) { if (oldAttr) { return "div-id" + index + oldAttr; } else { return "div-id" + index; } }) .each(function () { $("span", this).html("(id = ‘<b>" + this.id + "</b>‘)"); }); }); </script> </head> <body> <div id="someId">Zero-th <span></span></div> <div>First <span></span></div> <div>Second <span></span></div> </body> </html>
上面的例子,对应的页面结果如下:
<script type="text/javascript"> $(document).ready(function () { $("div").attr("id", function (index, oldAttr) { return undefined; }).each(function () { $("span", this).html("(id = ‘<b>" + this.id + "</b>‘)"); }); }); </script>
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function () { $("input[type=button]").click(function () { $("div").removeAttr("title"); }); }); </script> </head> <body> <input type="button" value="ClickMe"></input> <div title="hello">Zero</div> </body> </html>
jquery:http://www.itmmd.com/tag/jquery.html
jQuery教程(31)-jQuery插件开发之全局函数插件
jQuery教程(30)-jQuery插件开发之自定义选择符
jQuery教程(29)-jQuery插件开发之为插件方法指定参数
jQuery教程(28)-jQuery插件开发之使用插件
jQuery教程(27)-jQueryajax操作之修改默认选项
$element.prop( "onclick", null ); console.log( "onclick property: ", $element[ 0 ].onclick );
jquery学习笔记-jQuery操纵DOM元素属性 attr()和removeAtrr()方法
原文地址:http://blog.csdn.net/dyllove98/article/details/43018389