jQuery选择器介绍

jQuery选择器介绍

jQuery选择器分为:

#id,.class,element, selector1,selector2,selector3.....

以以下的html为例:

<body>

用户名:<input type="text" id="name" class="myCalss"/>

性别:<input type="text" name="sex" class="myCalss"/>

年龄:<input type="text" name="age" class="myCalss"/>

薪资:<input type="text" name="salary" class="myCalss"/>

出生日期::<input type="text" name="birth"/>

电话:<input type="text" name="phone" id="phone"/>

<div class="myClass" style="width:100,height:100,border="1px">

</div>

</body>

1. id选择器

//1.id选择器,根据它来获取指定的jQuery对象

//$("#id名称")获取指定的id的jQuery对象

var $uName = $("#name");//jQuery对象 是一个数组对象

//调用jQuery对象的方法 name名称 value值

$uName.css("color","red");//使得输入框的文字为red

$uName.css("border-color","blue");//使得文本框的元素为blue

2.emement选择器 元素节点 ---标签 获取到的是所有的指定的标签

var $inputs = $("input");//返回jQuery对象 是一个数组对象

//alert($inputs.size());//长度为6

//jQuery对象的遍历

$inputs.each(function(i,ipt){ //i相当于是for循环中的i 并且i++

//ipt是一个dom对象

if(i%2==0){

ipt.style.background="yellow";

}else{

ipt.style.background="blue";

}

});

3.class选择器 根据class获取jQuery对象

var $inpCls = $(".myCalss");

$inpCls.css("background-color","pink");

//遍历该对象

$inpCls.each(function(i,domCls){

if(i%2==0){

domCls.style.background="yellow";

}else{

domCls.style.background="blue";

}

});

4.selector1,selector2,selector3.....多向选择器

//是返回所有元素的一个jQuery对象

var $sels = $(".myClass,#phone");

alert($sels.size());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值