最近在用mui寫頁面,固然了在移動App裏引入jq或zepto這些框架,確定是極不理性的。
原生JS挺簡單,爲什麼須要jq?
jq的成功當時是由於ie六、七、八、九、十、chrome、ff這些瀏覽器不兼容,讓開發者崩潰,並且pc上瀏覽器性能好,跨平臺兼容也不影響性能。但jq根本就不是爲手機設計的。
手機上只有webkit瀏覽器(忽略wp,反正mui不支持wp),根本就不須要jq這種封裝框架來操做dom。css
//jq $('.el'); //js document.querySelector("div"); div.querySelectorAll('.el'); //mui mui(".el")[0];
//jq $('.el').parent(); $('.el').prev(); $('.el').next(); $('.el').last(); $('.el').first(); //js document.querySelector('.el').parentNode; document.querySelector('.el').previousElementSibling; document.querySelector('.el').nextElementSibling; document.querySelector(".el").lastElementChild; document.querySelector(".el").children[0];
//jq $('.el').html(); $('.el').val(); $('.el').text(); $(el).replaceWith(string); //js document.getElementById('el').innerHTML; document.getElementById('el').value; document.getElementById('el').textContent document.getElementById('el').outerHTML = string;
//jq var newEl = $('<div/>'); //js var newEl = document.createElement('div');
//jq $('.el').filter(':first').attr('key', 'value'); $('.el').filter(':first').attr('key'); $('.el').addClass('class'); $('.el').removeClass('class'); $('.el').toggleClass('class'); $(el).css('border-width', '20px'); //js document.querySelector('.el').setAttribute('key', 'value'); document.querySelector('.el').getAttribute('key'); document.querySelector('.el').classList.add('class'); document.querySelector('.el').classList.remove('class'); document.querySelector('.el').classList.toggle('class'); document.querySelector('.el').style.borderWidth = '20px';
//jq $('.el').append($('<div/>')); //js document.querySelector('.el').appendChild(document.createElement('div'));
//jq var clonedEl = $('.el').clone(); //js var clonedEl = document.querySelector('.el').cloneNode(true);
//jq $('.el').remove(); // js var toRemove = document.querySelector(el); toRemove.parentNode.removeChild(toRemove);
//jq $.get('url', function (data) { }); $.post('url', {data: data}, function (data) { }); //js // get var xhr = new XMLHttpRequest(); xhr.open('GET', url); xhr.onreadystatechange = function (data) { } xhr.send(); // post var xhr = new XMLHttpRequest() xhr.open('POST', url); xhr.onreadystatechange = function (data) { } xhr.send({data: data});
//jq $(el).show(); $(el).hide(); //js el.style.display = ''; el.style.display = 'none';
//jq $(el).hasClass(className); //js if (el.classList) el.classList.contains(className); else new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
//jq $(el).addClass(className); //js if (el.classList) el.classList.add(className); else el.className += ' ' + className;
//jq $(el).removeClass(className); //js // removeClass, takes two params: element and classname function removeClass(el, cls) { var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)"); el.className = el.className.replace(reg, " ").replace(/(^\s*)|(\s*$)/g,""); }
若是你只須要支持像IE10+,Chrome,FireFox,Opera和Safari這樣較現代的瀏覽器,那麼你能夠開始使用HTML5的classList功能,它讓增長和刪除類變得更簡單html
el.classList.add(className); el.classList.remove("foo") el.classList.contains("foo"); el.classList.toggle("active");
//jq $(el).before(htmlString); $(parent).append(el); $(el).after(htmlString); //js el.insertAdjacentHTML('beforebegin', htmlString); parent.appendChild(el); el.insertAdjacentHTML('afterend', htmlString);
//jq $(el).children(); //js el.children
//jq $.trim(string); //js string.trim();
參考:
http://youmightnotneedjquery.com/
http://www.quirksmode.org/dom/core/
http://blog.jobbole.com/52195/
http://ourjs.com/detail/535556a1ed9add0e26000002
http://www.cnblogs.com/lhb25/p/you-might-not-need-jquery.htmljquery