jQuery选择器简单应用
来源:互联网 发布:淘宝图片用什么软件 编辑:程序博客网 时间:2024/05/15 23:52
用Js写一些例如全选、反选等功能的时候,会感觉代码相当的多,而如果用jQuery选择器的话就会相当的简洁了
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>jQueryTest</title><script type="text/javascript" src="jquery-1.9.1.min.js"></script></head><body><table align="center" border="1"><tr><td><input type="checkbox"/></td></tr><tr><td><input type="checkbox"/></td></tr><tr><td><input type="checkbox"/></td></tr><tr><td><input type="checkbox"/></td></tr><tr><td><input type="checkbox"/></td></tr><tr><td><input type="button" value="全选中"/></td><td><input type="button" value="全取消"/></td><td><input type="button" value="反选"/></td></tr></table> <script type="text/javascript">$(":button:eq(0)").click(function(){$(":checkbox").attr("checked","checked").prop("checked",true);});$(":button:eq(1)").click(function(){$(":checkbox").removeAttr("checked").prop("checked",false);});$(":button:eq(2)").click(function(){var $notChecked=$(":checkbox:not(:checked)");var $checked=$(":checkbox:checked");$notChecked.attr("checked","checked").prop("checked",true);$checked.removeAttr("checked").prop("checked",false);});</script></body></html>
需要注意的是:
$(":button:eq(0)").click(function(){$(":checkbox").attr("checked","checked");});$(":button:eq(1)").click(function(){$(":checkbox").removeAttr("checked");});$(":button:eq(2)").click(function(){var $notChecked=$(":checkbox:not(:checked)");var $checked=$(":checkbox:checked");$notChecked.attr("checked","checked");$checked.removeAttr("checked");});在IE8经过测试上面这段Js代码可以运行,但在firefox上面运行就有问题,查看了一下官网上面的说明,发现如果是checkbox会比较特殊,需要
调用prop()来设置。
1
2
3
4
5
6
7
8
9
// Correct if changing the attribute is desired
$(elem).attr("checked", "checked");
// Correct for checking the checkbox
$(elem).prop("checked", true);
// Correct if removing the attribute is desired
$(elem).removeAttr("checked");
// Correct for clearing the checkbox
$(elem).prop("checked", false);
由于这里不能直接写入js代码,都被屏蔽了,所以只插入了一个没效果的多选表。
另附上jQuery的下载地址右击鼠标另存为就行了
另外可以上jQuery的官网上面去看http://jquery.com/
- jQuery选择器简单应用
- jquery选择器的简单应用
- Jquery简单过滤选择器(应用最广泛的选择器)
- jquery选择器之简单选择器
- jQuery选择器-简单选择器示例
- Jquery-选择器-简单的选择器
- jquery的选择器应用
- jquery 简单的选择器
- jquery简单选择器
- jQuery简单过滤选择器
- Jquery选择器——简单选择器
- jQuery 选择器和事件应用
- jQuery 自定义选择器及应用
- jquery中的简单过滤选择器
- jQuery 的选择器 简单总结
- JS简单实现JQuery选择器
- 简单的时间选择器插件(jquery版本)
- jquery 简单过滤选择器 遍历集合
- object-c中的assign retain copy
- 《傅雷家书》-文摘一-压力和焦虑
- 常见的各个平台下面的内存检查工具或者插件
- 机器学习与数据挖掘
- 第1天 打印9*9乘法表
- jQuery选择器简单应用
- android获取屏幕宽高的两种方法
- 随想录(写给那些学校不是985、211的同学们)
- 基于lucene搜索引擎的Hibernate Search,官方文档翻译
- 简明 Vim 练级攻略
- 字符串处理函数
- 多进程和多线程比较
- Windows编程 之临时文件 (笔记)
- XStream快速起步 简单方便实现序列化(XML和JSON)