jquery学习笔记-jQuery操纵DOM元素属性 attr()和removeAtrr()方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jquery学习笔记-jQuery操纵DOM元素属性 attr()和removeAtrr()方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5729字,纯文字阅读大概需要9分钟。
内容图文
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>
上面的例子,对应的页面结果如下:
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>
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 );
原文:http://blog.csdn.net/dyllove98/article/details/43018389
内容总结
以上是互联网集市为您收集整理的jquery学习笔记-jQuery操纵DOM元素属性 attr()和removeAtrr()方法全部内容,希望文章能够帮你解决jquery学习笔记-jQuery操纵DOM元素属性 attr()和removeAtrr()方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。