《鋒利的jQuery》學習筆記
-
if($(div).is('.class'))
同 if($(div).hasClass('class'))
- $(div).height($(div).height()+50) 同$(div).animate({height:'+=50'},400)
- 事件命名空間 $(div).bind('click.namespace'),取消事件$(div).unbind('.namespace')
- $(div).triggerHandler('focus')不一樣於$(div).trigger('focus'),不會得到焦點
-
$(div).addClass('test')
同$(div)['addClass']('test')
,所以可用變量$(div)[hasclass?'removeClass':'addClass']('test')
- 讓下拉框選中
$(select).val(['text1','text2'])
text1爲option值
- addClass('class')添加class,attr('class',class)會覆蓋class
- 刪除元素$(li).remove() $(li:eq(2)).detach()
- toggleClass()切換class,toggle()切換顯示隱藏
- 判斷元素是否存在if($(div).length > 0 )
- $('.one + div') 同$('.one').next('div'),$('.one ~ div') 同$('.one').nextAll('div')
-
$(div).click(functioin(){ if($(this).is(':visible')) { $(this).next().hide(); } else{ $(this).next().show(); } });
同$(div).toggle(function(){ $(this).next().hide(); }, function(){$(this).next().show();} );
也可直接 $(div).click(functioin(){$(this).next().toggle();});
- 移動元素$(li).appendTo($ul)
- slideToggle同slideUp,slideDown,fadeToggle同fadeIn,fadeOut
- hover同mouseover,mouseout但其實綁定的是mouseenter,mouseleave事件,所以觸發離開事件是trigger('mouseleave')
- jquery插件方法可寫在$.fn.extend({test:function(){}}) 或直接$.extend({test:function(){}}),注意其中的this爲jq對象,而不是dom對象
- $(form).searlize()將form對象序列化爲字符串$(form).searlizeArray()轉化爲json對象$.param({name:'sf',age:1})轉化爲name=sf&age=1
- 延遲觸發delay(1000)
- var str=$('p').map(function(){
return $(this).text()
}).get().join(',') 返回p標籤全部的內容
- $(:input) .attr('checked') 返回undefined
$(:input) .prop('checked')返回true false
$(:input) .prop('checked',true)
$(:input) .removeProp('checked')取消選中
第一次寫博客,有錯誤請指出,輕拍jquery
歡迎關注本站公眾號,獲取更多信息