1 id选择器(指定id元素)
将id="one"的元素背景色设置为黑色。(id选择器返单个元素)
$(document).ready(function () {
$('#one').css('background', '#000');
});
2 class选择器(遍历css类元素)
将class="cube"的元素背景色设为黑色
$(document).ready(function () {
$('.cube').css('background', '#000');
});
3 element选择器(遍历html元素)
将p元素的文字大小设置为12px
$(document).ready(function () {
$('p').css('font-size', '12px');
});
4 并列选择器
$(document).ready(function () {
// 将p元素和div元素的margin设为0
$('p, div').css('margin', '0');
});
5 基本过滤选择器
:first和:last(取第一个元素或最后一个元素)
$(document).ready(function () {
$('span:first').css('color', '#FF0000');
$('span:last').css('color', '#FF0000');
});
下面的代码,G1(first元素)和G3(last元素)会变色
<span>
G1
</span>
<span>G2</span>
<span>
G3
</span>