<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery02:$工具方法&属性css</title>
<script src="js/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
.xx {
border: solid red 5px;
}
.aa{
background-color: #FF0000;
}
.bb{
background-color: aquamarine;
}
</style>
<script type="text/javascript">
$(function() {
//1.工具方法
//$.each()遍历数组、对象的数据
//定义对象{}
// var stu={"name":"许杰","age":19};
//遍历对象
// console.info(stu.name,stu.age);
//可以不打印,但必须
// $.each(stu,function(k,v){//key,value
// console.info(v);
// })
//定义数组
// var names=["许杰","许明扬","许嵩"];
//遍历数组
// $.each(names,function(i,n){//下标,元素
// console.info(n);
// })
//定义对象数组[{},{}]
// var stus=[{"name":"王俊","age":19},{"name":"张嘉倪","age":20,}]
// $.each(stus,function(i,n){
// // console.info(n.name,n.age)//方式一
// // $.each(n,function(k,v){
// // console.info(v);
// // })//方式二
// })
// 1.2
// $.trim()去除前后空格
// var str=" zking ";
// console.info($.trim(str).length);
//1.3
// $.type()得到数据类型
// var str="aa";//string
// var stu={"name":"许杰","age":19};//object
// var stus=[{"name":"王俊","age":19},{"name":"张嘉倪","age":20,}]//array
// console.info($.type(stus));
//1.4
// $.isFunction()判断是否是函数
// console.info( $.isFunction(myf));//myf不能为myf(),否则会调用!
// $.isArray()判断是否是数组
// var stus=[{"name":"王俊","age":19},{"name":"张嘉倪","age":20,}]//array
// console.info($.isArray(stus))//ture
//1.6
// $.parseJSON()解析json字符串转换为js对象/数组
// var stustr='{"name":"许杰","age":19}';//对象字符串
// var stu=$.parseJSON(stustr);
// console.info($.type(stu));
// $.each(stu,function(k,v){
// console.info(v);
// })
var stus = '[{"name":"王俊","age":19},{"name":"张嘉倪","age":20}]' //array
//转出js对象数组
// console.info($.type(stus));
var stu = $.parseJSON(stus);
// console.info($.type(stu));
//遍历对象数组
// $.each(stu,function(k,v){
// console.info(v.name,v.age)
// })
//属性和css
// 2.1 attr()拿值 设值
// var mpath= $("#aa").attr("src");
// console.info(mpath);
// 设值
// $("#aa").attr("src","img/dd_arrow_down.jpg");
// $("#aa").attr("width","200px");
// 2.2 removeAttr()移除属性值
// $("#aa").removeAttr("src");
//2.3 动态增加样式addClass()
// $("#aa").addClass("xx");
//attr与addClass区别 前者是会xx覆盖yy,后者会xx与yy叠加
// $("#aa").attr("class","xx");
// 2.4 removeClass()删除样式
// $("#aa").removeClass("xx");
//给图片增加name值
// $("#aa").attr("name","xx");
// $("#aa").prop("name","xx");
//prop与attr区别 属性值是boolean时
// $("#ok").click(function(){
// $(".abc").attr("checked",true);
// })
// $("#ok").click(function(){
// $(".abc").attr("checked",false);
// })
$("#ok").click(function() {
$(".abc").prop("checked", true);
})
$("#nook").click(function() {
$(".abc").prop("checked", false);
})
//优化表格隔行换色
// $("table tr:even").addClass("aa");
// $("table tr:odd").addClass("bb");
//2.6 html()与text()区别
// var a=$("p").html();
// console.info(a);
// var b=$("p").text();
// console.info(b);
//2.7
// val() 拿值
// var tetx=$("#tex").val();
// console.info(text);
//赋值
// var tex=$("#tex").val("哈哈哈");
//css
// 设置值
// $("p").css("background","red");//单
// $("p").css("background","rgb(25,132,243)");//单rgb 三原色
// $("p").css("background","rgba(255,0,0,0.5)");//单 rgba a:透明度
$("p").css("background","#E25574");//单
// $("p").css({"background":"yellow","color":"red"});//多
// 拿值
// var a=$("p").css("background");
// console.info(a);
})
function myf() {
alert("1");
}
</script>
</head>
<body>
<p>心痛<span>only one</span></p>
<img src="img/advpic.jpg" width="300px" id="aa" class="yy">
<hr>
<input type="button" name="" id="ok" value="全选" />
<input type="button" name="" id="nook" value="取消全选" />
<input type="checkbox" class="abc" value="牛马" />牛马
<input type="checkbox" class="abc" value="牛" />牛
<hr>
<table border="1" width="50%" >
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type="text" name="" id="tex" value="" />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){
$("#all").click(function(){//先以全选id设点击事件
if ($("#all").is(":checked")) {//对象.checked得到的是空值,所以用.is()方法来判断其状态
$(".al").prop("checked",true);//使复选与全选状态保持一致;
} else{
$(".al").prop("checked",false);
}
});
//假设法
// for
// $(".al").click(function(){
// var f=true;
// for(var i=0;i<$(".al").length;i++){
// if ($(".al")[i].checked == false) {
// f=false;
// }
// }
// $("#all").prop("checked",f);
// })
//.each()
$(".al").click(function(){//
var f=true;
$.each($(".al"),function(k,v){
if ($(".al")[k].checked==false) {
f=false;
}
})
$("#all").prop("checked",f);
})
// // .length
// $(".al").click(function(){//拿到复选的点击事件
// if ($(".al:checked").length!=$(".al").length) {//复选的选中与未选中时长度不同
// $("#all").prop("checked",false);//以此设置全选的状态
// } else{
// $("#all").prop("checked",true);
// }
// })
})
</script>
</head>
<body>
<input type="checkbox" id="all" src="" />全选<br>
<input type="checkbox" class="al" src="" />
<input type="checkbox" class="al" src="" />
<input type="checkbox" class="al" src="" />
</body>
</html>