window.onload = function(){???//原生js設置背景屬性
var oBox = document.getElementById('box');
oBox.onclick = function(){
this.style.background = 'yellow';
}
}
$(function(){????//jquery寫法
$('#box').click(function(){
$(this).css('background','yellow')
})
})
$(function(){???//混合寫法,get()!
$('#box').click(function(){
$(this).get(0).style.background = 'yellow'
})
})
若有li彈出它的長度,方法如下:
alert($('li').size())
alert($('li').length)
設置最后一個元素的背景:
$('li').eq($('li').size()-1).css('background','yellow')
$('li').eq(-1).css('background','yellow')
彈出DIV的寬:
alert($('#box').css('width'))??//conetnt
alert($('#box').outerWidth())??//padding + content
alert($('#box').outerWidth(true)) //padding + margin + content
高:
alert($('#box').css('width'))??//conetnt
alert($('#box').outerheight())??//padding + content
alert($('#box').outerheight(true)) //padding + margin + content
Text 和 HTML 的區別:
Hello ! star I'm sky
alert($('#txt').text()); //彈出Hello ! star I'm sky
alert($('#txt').html()); //彈出Hello ! star I'm sky
$('p').text('Tomorrow'); //添加標簽i后Tomorrow
$('p').html('Tomorrow'); //Tomorrow
移除再拉回:
var box = $('#box'); //方法1
$('#box').remove();??//remove可以用detach代替
$('body').append(box);
var box = $('#box').detach(); //方法2 同上
$('body').append(box);
detach 會有一個返回值$('#box'),直接存在變量里
parent的注意事項:
$('#son').parent().css('backgroundColor','green');//獲取父級標簽
$('#son').parents().css('backgroundColor','green');//父及 及以上同時可傳選擇器
$('#son').parents('.lala').css('background','black')//父元素中有lala標簽的
//最貼近自己的父及,且必須傳參,僅能找到最近的父級標簽
$('#son').closest('lala').css('background','black')
兄弟節點siblings:
//不傳參就是不包括自身,傳參就是過濾后找到那個參數
$('.two').siblings().css('background','pink')
$('.two').siblings('.one').css('background','pink')
//next僅能返回一個對象,可以寫多個next
$('.two').next().next().next().css('background','pink')
//返回一組對象可以傳參,不傳參就是下面的全都被選中
$('.two').nextAll('.one').css('background','pink')
//從two到four之間的對象都被選中,不傳參下面的都被選中同nextAll不傳參一樣
$('.two').nextUntil('four').css('background','pink')
//過濾,除了dd其他都改變,not則相反
$('li').css('background','pink').filter('.dd').css('background','yellow')
$('li').css('background','pink').not('.dd').css('background','yellow')
wrap:(只能打包同一種標簽)
$('span').wrap('
$('span').wrapAll('
$('span,p').wrapAll('
$('span').unwrap().unwrap()//拆包