1、jQuery的属性操作
jQuery属性操作
HTML代码/文本/值 html([val|fn]) text([val|fn]) val([val|fnlarr]
htm()
它可以设置或获取起始标签和结束标签中的内容
和dom属性innerHTML一样
text()
它可以设置或获取起始标签和结束标签中的文本
和dom属性innerHTML一样
test.html
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript"> $(function (){ //不传参数是获取,传递参数是设置 // alert($("div").html() );//获取 // $("div").html("<h1>我是div中的标题 1</h1>");//设置 //不传参数是获取,传递参数是设置 // alert($("div").text());//获取 // $("div").text("<h1>我是div中的标题 1</h1>");//设置 //不传参数是获取,传递参数是设置 $("button").click(function () { alert($("#username").val());//获取 $("#username").val("超级程序员");//设置 }); }); </script> </head> <body> <div>我是div标签<span>我是div中的span</span></div> <input type="text" name="username" id="username"> <button>操作输入框</button> </body> </html>
val() 它可以设置或获取表单项的value属性值
和dom属性value一样
val方法同时设置多个表单项的值示例代码:
test2.html
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript"> $(function (){ /* //批量操作单选 $(":radio").val(["radio2"]); //批量操作筛选框中的选中状态 $(":checkbox").val(["checkbox3","checkbox2]); //批量操作多选下拉框中的选中状态 $("#multiple").val(["mul2","mul3","mul4"]); //批量操作单选下拉框中的选中状态 $("#single").val("sin2"); */ $(":radio,#single,:checkbox,#multiple").val(["radio2","checkbox3","checkbox2","mul2","mul3","mul4","sin2"]); }); </script> </head> <body> 单选: <input name="radio" type="radio" value="radio1" />radio1 <input name="radio" type="radio" value="radio2" />radio2 <br/> 多选: <input name="checkbox" type="checkbox" value="checkbox1" />checkbox1 <input name="checkbox" type="checkbox" value="checkbox2" />checkbox2 <input name="checkbox" type="checkbox" value="checkbox3" />checkbox3 <br/> 下拉多选; <select id="multiple" multiple="multiple" size="4"> <option value="mull">mul1</option> <option value="mul2">mul2</option> <option value="mul3">mu13</option> <option value="mul4">mul4</option> </select> <br/> 下拉单选: <select id="single"> <option value="sin1">sin1</option> <option value="sin2">sin2</option> <option value="sin3">sin3</option> </select> </body> </html>
属性
attr(namelpro|key,val|fn)
removeAttr(name)
prop(name|pro|key,val|fn)
removeProp(name)
attr()
可以设置和获取属性的值,
不推荐操作checked、readOnly、selected、disabled等等
attr方法还可以操作非标准的属性。比如自定义属性:abc、bbj
prop()
可以设置和获取属性的值,
只推荐操作checked、readOnly、selected、disabled等等
test3.html
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript"> $(function (){ //attr // alert($(":checkbox:first").attr("name"));//获取 // $(":checkbox:first").attr("name","abc");//设置 // alert($(":checkbox").attr("checked"));//官方觉得返回undefined是个错误 // alert($(":checkbox").prop("checked"));//false $(":checkbox:first").attr("abc","abcValue"); alert($(":checkbox:first").attr("abc")); }); </script> </head> <body> 多选: <input name="checkbox" type="checkbox" value="checkbox1" />checkbox1 <input name="checkbox" type="checkbox" value="checkbox2" />checkbox2 </body> </html>
2、JQuery练习
2、全选、全不选、反选
ex-4.html
<!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>Insert title here</title> <script type="text/javascript" src="../../script/jquery.js"></script> <script type="text/javascript"> $(function () { //给全选绑定单击事件 $("#checkedAllBtn").click(function () { $(":checkbox").prop("checked",true); }); //给全不选绑定单击事件 $("#checkedNoBtn").click(function () { $(":checkbox").prop("checked",false); }); //给全反选绑定单击事件 $("#checkedRevBtn").click(function () { //查询全部的球类复选框 $(":checkbox[name='items']").each(function () { this.checked=!this.checked; }); //要检查是否满选 //获取全部的球类个数 var allCount=$(":checkbox[name='items']").length; //再获取选中的球类个数 var checkedCount=$(":checkbox[name='items']:checked").length; // if (allCount==checkedCount){ // $("#checkedAllBox").prop("checked",true); // }else { // $("#checkedAllBox").prop("checked",false); // } $("#checkedAllBox").prop("checked",allCount==checkedCount); }); //给提交按钮单击事件 $("#sendBtn").click(function () { //获取选中的球类的复选框 var checkedCount=$(":checkbox[name='items']:checked").each(function () { alert(this.value); }); }); //给【全选/全不选】按钮绑定单击事件 $("#checkedAllBox").click(function () { //在事件的function中有一个this对象,表示当前正在响应的dom对象 // alert(this.checked); $(":checkbox[name='items']").prop("checked",this.checked); }); //给全部球类都绑定单击事件 $(":checkbox[name='items']").click(function () { //要检查是否满选 //获取全部的球类个数 var allCount=$(":checkbox[name='items']").length; //再获取选中的球类个数 var checkedCount=$(":checkbox[name='items']:checked").length; $("#checkedAllBox").prop("checked",allCount==checkedCount); }); }); </script> </head> <body> <form method="post" action=""> 你爱好的运动是?<input type="checkbox" id="checkedAllBox"/>全选/全不选 <br/> <input type="checkbox" name="items" value="足球"/>足球 <input type="checkbox" name="items" value="篮球"/>篮球 <input type="checkbox" name="items" value="羽毛球"/>羽毛球 <input type="checkbox" name="items" value="乒乓球"/>乒乓球 <br/> <input type="button" id="checkedAllBtn" value="全 选"/> <input type="button" id="checkedNoBtn" value="全不选"/> <input type="button" id="checkedRevBtn" value="反 选"/> <input type="button" id="sendBtn" value="提 交"/> </form> </body> </html>
4、DOM的增删改
内部插入
append(content|fn)
appendTo(content)
prepend(content|fn)
prependTo(content)
外部插入
after(content|fn)
before(content|fn)
insertAfter(content)
insertBefore(content]
替换
replacewith(content|fn)
replaceAl(selector)
删除
empty()
remove([expr])
内部插入:
appendTo()
a.appendTo(b)
把a插入到b子元素末属,成为最后一个子元素
prependTo/)
a.prependTo(b)
把a插到b所有子元素前面,成为第一个子元素
外部插入: insertAfter()
a.insertAfter(b)
得到ba
insertBefore()
a.insertBefore(b)
得到ab
替换: replaceWith()
a.replaceWith(b)
用b替换掉a
replaceAl()
a.replaceAl(b)
用a替换掉所有b
删除:
remove()
a.remove();
删除a标签
empty()
a.empty();
清空a际签里的内容
test3.html
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript"> $(function (){ //attr // alert($(":checkbox:first").attr("name"));//获取 // $(":checkbox:first").attr("name","abc");//设置 // alert($(":checkbox").attr("checked"));//官方觉得返回undefined是个错误 // alert($(":checkbox").prop("checked"));//false // $(":checkbox:first").attr("abc","abcValue"); // alert($(":checkbox:first").attr("abc")); //------------------------------------------- // $("<h1>标题</h1>").appendTo($("div")); // $("<h1>标题</h1>").insertAfter($("div")); // $("<h1>标题</h1>").insertBefore($("div")); // $("div").replaceWith( $("<h1>标题</h1>") ); // $("<h1>标题</h1>").replaceAll("div"); // $("div").remove(); $("div").empty(); }); </script> </head> <body> 多选: <input name="checkbox" type="checkbox" value="checkbox1" />checkbox1 <input name="checkbox" type="checkbox" value="checkbox2" />checkbox2 <br/> <br/> <div>1234</div> <div>1234</div> </body> </html>
从左到右,从右到左.html
<!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>Insert title here</title> <style type="text/css"> select { width: 100px; height: 140px; } div { width: 130px; float: left; text-align: center; } </style> <script type="text/javascript" src="../../script/jquery.js"></script> <script type="text/javascript"> //页面加载完成之后 $(function () { //第一个按钮【选中添加到右边】 $("button:eq(0)").click(function () { $("select:eq(0) option:selected").appendTo($("select:eq(1)")); // $("select[name=sel01] :selected").each(function () { //alert(this); // $(this).appendTo("select[name=sel02]"); // }); }); //第二个按钮【全部添加到右边】 $("button:eq(1)").click(function () { $("select:eq(0) option").appendTo( $("select:eq(1)") ); // $("select[name=sel01] option").each(function () { //alert(this); // $(this).appendTo("select[name=sel02]"); // }); }); //第三个按钮【选中删除到左边】 $("button:eq(2)").click(function () { $("select:eq(1) option:selected").appendTo($("select:eq(0)")); // $("select[name=sel02] :selected").each(function () { //alert(this); // $(this).appendTo("select[name=sel01]"); // }); }); //第四个按钮【全部删除到左边】 $("button:eq(3)").click(function () { $("select:eq(1) option").appendTo($("select:eq(0)")); // $("select[name=sel02] option").each(function () { //alert(this); // $(this).appendTo("select[name=sel01]"); // }); }); }); </script> </head> <body> <div id="left"> <select multiple="multiple" name="sel01"> <option value="opt01">选项1</option> <option value="opt02">选项2</option> <option value="opt03">选项3</option> <option value="opt04">选项4</option> <option value="opt05">选项5</option> <option value="opt06">选项6</option> <option value="opt07">选项7</option> <option value="opt08">选项8</option> </select> <button>选中添加到右边</button> <button>全部添加到右边</button> </div> <div id="right"> <select multiple="multiple" name="sel02"> </select> <button>选中删除到左边</button> <button>全部删除到左边</button> </div> </body> </html>
