如果你只需要针对现代浏览器,很多功能使用原生的 JavaScript 就可以实现。

DOM Selectors

//jQuery
var ele = $("#id .class");
//native javascript
var ele = document.querySelectorAll("#id .class");
//or native javascript
var ele1 = document.getElementById("id");
var ele = ele1.getElementsByClassName("ele1");

DOM 操作

Create elements

//jQuery
var newEl = $('<div />');
//native javascript
var newEl = document.createElement('div');

Append

//jQuery
#("#container").append("<span>content</span>");
//native javascript
var span = document.createElement("span");
span.appendChild(document.createTextNode("content"));
document.getElementById("container").appendChild(span);
//or native javascript, jQuery uses the native innerHTML method
document.getElementById("container").innerHTML += "<span>content</span>";

remove all child nodes

//jQuery
$("container").empty();
//The native equivalent using innerHTML
document.getElementById("container").innerHTML = null;
//or native javascript using removeChild
var c = document.getElementById("container");
while(c.lastChild) c.removeChild(c.lastChild);

remove the whole elemet from the DOM

//jQuery
$("#container").remove();
//native javascript
var c = document.getElementById("container");
c.parentNode.removeChild(c);

Clone the whole element from the DOM

//jQuey
var cloneEl = $("#container").clone();
//native javasript
var cloneEl = document.getElementById("container").cloneNode(true);

 parent

//jQuery
$('#el').parent();
//ntive javascript
document.getElementById('el').parentNode;

prev/next element

//jQuery
$("#el").prev();
$("#el").next();
//native javascript
document.getElementById("el").previousElementSibling;
document.getElementById("el").nexElementSibling;

css manipulation

class manipulation

//jQuery
$("#ele").addClass("myclass");
$("#ele").removeClass("myclass");
$("#ele").toggleClass("myclass");
//native javascript
function addClasses(node, cla){
if(!node.length) node = [node];
for(var n=0,m=node.length;n<m;n++){
if((" "+node[n].className+" ").indexOf(" "+cla+" ") <0){
node[n].className += " "+cla;
}
}
} function removeClass(node, cla){
if(!node.length) node = [node];
for(var n=0,m=node.length;n<m;n++){
if((" "+node[n].className+" ").indexOf(" "+cla+" ") >= 0){
node[n].className = (" "+node[n].className+" ").replace(" "+cla+" ", " ").replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "" );
}
} }
//remove myclass to all nodes
removeClass(document.querySelectorAll("p"), "myclass"); function toggleClass(node, cla){
if(!node.length) node = [node];
for(var n=0,m=node.length;n<m;n++){
if((" "+node[n].className+" ").indexOf(" "+cla+" ") >= 0){
node[n].className = (" "+node[n].className+" ").replace(" "+cla+" ", " ").replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "" );
}else{
node[n].className += " "+cla;
}
}
}
//toggle myclass to all nodes
toggleClass(document.querySelectorAll("p"), "myclass");
//or native javascript using classList
document.getElementById("ele").classList.add("myclass");
document.getElementById("ele").classList.remove("myclass");
document.getElementById("ele").classList.toggle("myclass");

style manipulation

//jQuery
$("#ele").css({
color: "#c00"
})
//native javascript
var ele = document.getElementById("ele");
ele.style.color = "#c00";

set/get attribute

//jQuery
$('#ele').attr('key', 'value');
$('#ele').attr('key');
//native javascript
document.getElementById("ele").setAttribute('key', 'value');
document.getElementById("ele").getAttribute("key");

event handling

document ready

//jQuery
$(start)
//native javascript
document.addEventListener("DOMContentLoaded", start);

forEach

//jQuery
$("p").each(function(i){
console.log("index " + i + ": " + this);
});
//native javascript
[].forEach.call(document.getElementsByTagName("p"),function(obj,i){consol.log("index "+i+": "+obj)})

Events

//jQuery
$('.el').on('event', functio(){ });
//Native javascript
[].forEach.call(document.querySelectorAll('.el'), function(el){
el.addEventListener('event', function(){
}, false);
});

Ajax

//jQuery
$.get('url', function(data){
});
$.post('url', {data: data}, function(data){ });
//native javascript
//get
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function(){ }
xhr.send();
//post
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.onreadystatechange = function(){ }
xhr.send({data: data});

相关文章:

Native JavaScript Equivalents of jQuery Methods: the DOM and Forms

Native JavaScript Equivalents of jQuery Methods: Events, Ajax and Utilities

使用原生JavaScript的更多相关文章

  1. 原生javascript 实现 animate

    原生javascript 实现 animate //animate function getstyle(obj,name){ if(obj.currentStyle){ return obj.curr ...

  2. 浅谈 原生javaScript&&react 实现全局触摸按钮(附带对addeventlistener的了解)

    1.采用原生javaACript 实现全局触摸按钮 首先在控制台输出,观察事件有哪些关于触摸的字段可以使用,然后拿这些字段的数据开始来写方法. 因为要做的是全局触摸按钮,我需要拿到的是按钮时时的坐标位 ...

  3. 你可能不需要 jQuery!使用原生 JavaScript 进行开发

    很多的 JavaScript 开发人员,包括我在内,都很喜欢 jQuery.因为它的简单,因为它有很多丰富的插件可供使用,和其它优秀的工具一样,jQuery 让我们开发人员能够更轻松的开发网站和 We ...

  4. 原生JavaScript技巧大收集(11~20)-(终于又被我找到这篇文章了)

    11.原生JavaScript加入收藏夹 function AddFavorite(sURL, sTitle) { try { window.external.addFavorite(sURL, sT ...

  5. 原生javascript加载运行

    原生javascript加载运行 (function(){ //TODO sometings }()); 在要运行相应代码的位置加入script标签,创建函数并自执行; 关于window.onload ...

  6. 原生javascript模仿win8等待进度条。

    一.序言 一直很中意win8等待提示圆圈进度条.win8刚出来那会,感觉好神奇!苦于当时没思路,没去研究.通过最近网上找找资料,终于给搞出来了!先上Demo,献丑了!预览请看:win8进度条. 二.简 ...

  7. 表单美化-原生javascript和jQuery单选按钮(兼容IE6)

    最近很多人问怎么美化表单的元素,大家都知道表单元素在各个浏览器中的表现不一,反正也是特别的丑,那么问题就来了,我们能自己设计表单元素的外观么?答案是可以的,现在我们就来试试吧.我们用两种方式来实现这一 ...

  8. javascript学习-原生javascript的小特效(原生javascript实现链式运动)

    以下代码就不详细解析了,在我之前的多个运动效果中已经解析好多次了,重复的地方这里就不说明了,有兴趣的童鞋可以去看看之前的文章<原生javascript的小特效> <!DOCTYPE ...

  9. javascript学习-原生javascript的小特效(多个运动效果整理)

    以下代码就不详细解析了,在我之前的多个运动效果中已经解析好多次了,重复的地方这里就不说明了,有兴趣的童鞋可以去看看之前的文章<原生javascript的小特效> <!DOCTYPE ...

  10. 原生javascript开发仿微信打飞机小游戏

    今天闲来无事,于是就打算教一个初学javascript的女童鞋写点东西,因此为了兼顾趣味性与简易程度,果断想到了微信的打飞机小游戏.. 本来想用html5做的,但是毕竟人家才初学,连jquery都还不 ...

随机推荐

  1. 基于.net mvc的校友录(四、系统结构图)

    这是整个系统结构的预览,话不多说,给个图: 本网站努力为每个人提供一个有效的校友录系统,为参与者提供一个简单有效的交流互动的平台,操作上要求简单.高效,性能上要求稳定.可扩展.在对同类网站系统进行了调 ...

  2. TFS更新

    我们小组决定将对学长的代码提出改进意见贯穿整个任务的整个过程,随时更新任务进度. 共计预计项目时间为58小时. 每个人都能够达到5到10小时的工作量.

  3. Crawling is going on - Alpha版本测试报告

    [Crawling is going on - Alpha版本] 测 试 报 告 文件状态: [] 草稿 [√] 正式发布 [] 正在修改 报告编号: 当前版本: 1.0.2 编写人: 周萱.林谋武. ...

  4. android开发 实现同时显示png/jpg 等bitmap图片还可以显示gif图片,有效防止OOM

    本来使用第三方jar包 GifView.jar  发现使用的时候不能显示png图片,而且多次setgifimage的时候还会OOM: 现在使用了一个新的第三方,demo是别人的, 下载链接:http: ...

  5. 【Construct Binary Tree from Preorder and Inorder Traversal】cpp

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...

  6. 【Count and Say】cpp

    题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...

  7. Testing Multi-Tenancy on a Local Machine

    If you are running locally and do not have a domain to map, you can edit your\Windows\System32\drive ...

  8. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  9. Matlab稀疏矩阵

    一.矩阵存储方式 MATLAB的矩阵有两种存储方式,完全存储方式和稀疏存储方式 1.完全存储方式 将矩阵的全部元素按列存储,矩阵中的全部零元素也存储到矩阵中. 2.稀疏存储方式 仅存储矩阵所有的非零元 ...

  10. Beginners Guide To Learn Dimension Reduction Techniques

    Beginners Guide To Learn Dimension Reduction Techniques Introduction Brevity is the soul of wit This ...