Jquery style selector and operation

jquery selector

Usage thought a jquery
select a page element, and then do something with it

jquery selector
jquery selector may quickly select elements, and the same selection rules css style, use the length property is determined whether a successful selection.

$('#myId') //选择id为myId的网页元素
$('.myClass') // 选择class为myClass的元素
$('li') //选择所有的li元素
$('#ul1 li span') //选择id为为ul1元素下的所有li下的span元素
$('input[name=first]') // 选择name属性等于first的input元素

Set selection filter

$('div').has('p'); // 选择包含p元素的div元素
$('div').not('.myClass'); //选择class不等于myClass的div元素
$('div').filter('.myClass'); //选择class等于myClass的div元素
$('div').eq(5); //选择第6个div元素

Select Set transfer

$('div').prev(); //选择div元素前面紧挨的同辈元素
$('div').prevAll(); //选择div元素之前所有的同辈元素
$('div').next(); //选择div元素后面紧挨的同辈元素
$('div').nextAll(); //选择div元素后面所有的同辈元素
$('div').parent(); //选择div的父元素
$('div').children(); //选择div的所有子元素
$('div').siblings(); //选择div的同级元素
$('div').find('.myClass'); //选择div内的class等于myClass的元素

Determine whether to select the elements
jquery there is fault tolerance, even if the element is not found, it will not go wrong, you can use the length property to determine whether the found elements, length equal to 0, that is not selected to the elements, length greater than 0, is selected to the elements.

var $div1 = $('#div1');
var $div2 = $('#div2');
alert($div1.length); // 弹出1
alert($div2.length); // 弹出0
......
<div id="div1">这是一个div</div>

jquery style operations

jquery usage Thought in
the completion of the assignment and the value of the same function

Operating line between style

// 获取div的样式
$("div").css("width");
$("div").css("color");

//设置div的样式
$("div").css("width","30px");
$("div").css("height","30px");
$("div").css({fontSize:"30px",color:"red"});

Special attention
more element selectors get access to the information obtained is the first one, for example: $ ( "div") css ( "width"), is the first to obtain a div of width..

Operating style class name

$("#div1").addClass("divClass2") //为id为div1的对象追加样式divClass2
$("#div1").removeClass("divClass")  //移除id为div1的对象的class名为divClass的样式
$("#div1").removeClass("divClass divClass2") //移除多个样式
$("#div1").toggleClass("anotherClass") //重复切换anotherClass样式

Guess you like

Origin www.cnblogs.com/MessiXiaoMo3334/p/11313649.html