1、parent()和offsetParent()
區(qū)別,parent()只是獲取父級元素,不管父級元素怎樣,而offsetParent()則是獲取有定位的父級,如果一直沒有就會一直找到body。
div#parent{
width:100px;
height:100px;
margin:30px;
background:red;
/* position:absolute; */
}
div#child{
width:50px;
height:50px;
margin:20px;
background:green;
}
<script>
$('#child').offsetParent().css('background','black')
</script>
因為child的父級parent沒有定位,所以會一直往上找到body把body的背景顏色改成了黑色。
2、val()
$('input').val('我不想提交')
空就是獲取,如果不是空就是設(shè)置value值
3、size()
$('li').size()
不接收參數(shù),只是獲取JQ返回的那個偽數(shù)組的長度。
4、each()
先不說each()這個方法,先記錄一下剛才試驗過程中遇到的問題。
- 用$()來取整個document的時候,括號里面不能加引號,加了引號就取不到,在stackoverflow上查了一下,也沒有引號,但是沒說具體原因。
- 用jQuery方法來取顏色的時候,默認取到的是rgb格式的。
- body也是一個block元素,內(nèi)容也需要其它東西來撐起來的,而document則不需要。
- document.documentElement這個API取到的是document的中的根元素,一般來說就是html。
<script>
$('li').each(function(index,element){
element.style.background = 'red'
element.textContent = index
})
</script>
each()接收一個函數(shù),這個函數(shù)接收兩個參數(shù),一個是遍歷的元素的索引,一個是元素本身。這個API很有用。