1.1 jquery和JavaScript可以共存,但是不能夠混用!
<div id="box"></div>
<script>
$(document).ready(function () {
$('#box').get(0).onclick = function(){
this.style.background = blue;
}
})
$('.box')[0].style.background = 'yellow';
</script>
1.2 length
length雖然是原生js的一個屬性,但是length也是jquery里面封裝過的方法,可以直接使用!但是個人建議你,如果需要獲取長度的時候還是去使用size()方法。
1.3 css屬性值
parseInt($('.box').css('marginTop'));//margin-top
$('.box').width();//content
$('.box').outerWidth();//content+padding+border
$('.box').innerWidth();//content+padding
($('.box').outerWidth()-$('.box').innerWidth())/2;//border
($('.box').outerWidth()-$('.box').width())/2;//padding+border
($('.box').innerWidth()-$('.box').width())/2;//padding ($('.box').outerWidth(true)-$('.box').outerWidth())/2;//margin
1.4 text()與html()
text僅僅返回 標(biāo)簽內(nèi)部的內(nèi)容 不會返回標(biāo)簽,負值也是這樣
<p id="para">I am para <span>I am span</span> !</p>
<script>
alert($('#para').tetx());
</script>
html可以返回標(biāo)簽,但是只能返回第一個!
<p>I am para <span>I am span</span> !</p>
<p>I am para aaa <span>I am span</span> !</p>
<script>
alert($('p').html());
</script>
設(shè)置值的話,可以一起設(shè)置
<p>I am para <span>I am span</span> !</p>
<p>I am para aaa <span>I am span</span> !</p>
<script>
$('p').html('<h1>das</h1>');
</script>
1.5remove() 和 detach()
我們先寫一個div 并為其添加一個click事件。
$('#box').click(function(){
alert(1);
})
如果在開發(fā)的時候 需要刪除它!但是在后面的操作當(dāng)中可能又要遇到div的添加它
var box = $('#box').remove();
$('body').append(box)
但是我們再次點擊。發(fā)現(xiàn)事件沒有了!
為了解決這樣的bug!我們就必須使用detach()了;
$('#box').click(function(){
alert(1);
})
var box = $('#box').detach();
$('body').append(box)
1.6 $() 和 window.onload = function(){}
$() //是等DOM加載完畢
window.onload = function(){}. //全部加載完畢