jQuery操纵DOM元素属性 attr()和removeAtrr()方法使用详解
jQuery操纵DOM元素属性 attr()和removeAtrr()方法使用详解
attr()方法 读操作
<!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>
即可分别获取每个元素的属性.
attr()方法 写操作
<!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>
removeAttr()方法
<!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>
$element.prop( "onclick", null ); console.log( "onclick property: ", $element[ 0 ].onclick );
参考资料
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。