detach():這個方法不會把匹配的元素從jQuery對象中刪除,於是能夠在未來再使用這些匹配的元素。與remove()不一樣的是,全部綁定的事件、附加的數據等都會保留下來。javascript
jquery ajax不能下載文件 css
jquery 使用$.cookie:須要引入jquery.cookie.js;html
不能屢次引入jquery.jsjava
場景:polymer paper-button選中,元素添加active屬性;如何判斷元素是否被active:$('#').attr('active') ===undefined,說明沒有被activejquery
選擇有active屬性的元素$('div[active]');ajax
jquery 同時綁定click和dblclick,dblclick時也會觸發click:http://www.cnblogs.com/huangjacky/archive/2012/09/27/2706110.htmljson
var _time = null; $(this).find("tr").dblclick(function(e){ clearTimeout(_time); console.log("dblclick"); //真正雙擊代碼 }).click(function(e){ clearTimeout(_time); _time = setTimeout(function(){ console.log("click"); //單擊事件在這裏 }, 300); });
jquery插件判斷方法跨域
if(!window.jQuery) { alert("Could not find jQuery! The HTML must include jquery.js before opencpu.js!") }
jquery 操做 checkbox瀏覽器
國內不少技術博客太坑了,jquery選中或取消checkbox,居然全都寫錯了,都用attr('', true/'checked');大家也不本身多點幾回,能行得通嗎?緩存
或許是我用的jquery版本不一樣,我用的1.11.3
$('').prop('checked', true); $('').prop('checked', false);
jquery 操做 radio
$('input[name="color_pal"][value="sfp"]').prop('checked', true);
jquery hover:這個方法挺好用的,若是須要hover 和 no hover有不一樣的事件時,能夠用它來實現
$('').hover(function(){}, function(){})
jquery .stop();
中止當前元素上運行的動畫
須要的場景:mouse over 執行一個動畫;若是很快over5次,會執行5次動畫;若是使用了.stop(), 則在動畫執行期間不會監聽到over事件
jquery對象與dom對象
DOM對象:var dom = document.getElementById('#id'); 方法:dom.innerHTML;
jQuery對象:var jquery = $('#id'); 方法:jquery.html()
jquery對象轉爲dom對象:[0] get(0)
dom對象轉爲jquery對象:$(dom)
click <a>
問題場景:須要js觸發<a>的click()事件,使用jquery對象一直沒有成功的觸發:$('#test').click()
解決辦法:把jquery對象轉爲dom對象,$('#test')[0].click(); $('#test').get(0).click(); 這裏至關於調用了dom的click()方法
使用jquery插件dotdotdot
$(".test").dotdotdot({ wrap: 'letter' }); // 英文的話,wrap是Word和letter均可以,若是是中文的話,wrap只能是letter
animate延遲實現
錯誤的寫法
setTimeout(function(){ $(this).animate({top: '0'}, 400, 'linear', function(){ console.log('debug2'); }) }, 200)
正確的寫法
setTimeout(function(){ $('#test').animate({top: '0'}, 400, 'linear', function(){ console.log('debug2'); }) }, 200)
緣由:setTimeout會改變上下文環境,致使this指向有問題
不是接受鍵盤事件,而是模擬鍵盤操做
觸發keydown事件
jQuery(function($){ $(window).keydown(function(e){ if(e.keyCode==13){ e.preventDefault(); alert("按下了enter`````"); } }); function simulateKeyPress(character){ var e = jQuery.Event("keydown"); e.keyCode=character; $(window).trigger(e); }; $('#submit').on('click', function(){ simulateKeyPress(13); })
jquery mobile
input click後,出現藍色的邊框
input:focus { outline: none; } 可消除。
今天碰到一個js的加載問題。
描述:<script>引入jquery,再引入opencpu。若是opencpu先加載完成,就會開始執行,若是沒找到jquery,就會提示報錯;不然,執行setURL()
解決辦法:加載順序,經常使用require,可是這裏用的比較簡單,感受不必。
一、加載完jquery,再經過js動態加載opencpu。這種方法,本身寫的動態加載的函數沒有回調,無法在opencpu加載完,執行setURL。
二、jquery ajax getScript(),能夠彌補1的不足。
jQuery.ajax({ url: "jquery.cookie.js", // 能夠緩存腳本 dataType: "script", cache: true }).done(function() { jQuery.cookie("cookie_name", "value", { expires: 7 }); });
jsonp用法:只支持get 不支持post
$.ajax({ type: 'POST', url: "http://localhost:3050/test", data: { "Email": "asda@qeq.com", "Password": "123456" }, dataType: "jsonp", success: function(data){ debugger } })
跨域 須要在服務器端添加Access-Control-Allow-origin:*
當調整瀏覽器窗口大小時,觸發resize時間;而內容高度變化時,不會觸發resize事件
innerHeight() 就是scrollHeight
jquery easing
須要添加ui.css ui.js才能夠用緩動函數;經常使用的緩動函數,能夠從官網查找:http://jqueryui.com/easing/
fullpage.js 開發時,把全部的內容都放在div,而後div居中就好。
.stop()的一個問題
代碼以下
$('.page-6 .list .item').mouseenter(function(){ var that = this; $(this).find('.img-wrap').stop(true).fadeOut(200, function(){ $(that).find('.text-wrap').stop(true).fadeIn(200, function(){ }); }); }) $('.page-6 .list .item').mouseleave(function(){ var that = this; $(this).find('.text-wrap').stop(true).fadeOut(200, function(){ $(that).find('.img-wrap').stop(true).fadeIn(200, function(){ }); }); })
若是使用jquery-1.7.2.js 當在200ms內,leave元素時,相關元素不會回到最初狀態;若是使用jquery-2.1.4.js 當在200ms內,leave元素時,相關元素會回到最初狀態,這纔是理想的狀況。
innerHeight() 包括padding