zl程序教程

您现在的位置是:首页 >  前端

当前栏目

jquery库文件略庞大用纯js替换jquery的方法

JSjQuery文件方法 替换 庞大 用纯
2023-06-13 09:15:42 时间

jquery库文件略庞大,在某些情况下,需要尽量减少加载的文件(文件大小),需要用纯js来编写效果

$("#layer")
document.getElementById("layer")

$("#layerspan")
varlayer=document.getElementById("layer");
varspan=layer.getElementsByTagName("span");

$("#inner").parent()
document.getElementById("inner").parentNode

$(window).width();
document.body.clientWidth

$("#layer").width();
document.getElementById("layer").style.width

$("#wrap").append("<span>a</span>");
varspan=document.createElement("span");
span.innerHTML="a";
document.getElementById("wrap").appendChild(span);

$("#wrapspan").remove();
deleteSpan();
functiondeleteSpan(){
varcontent=document.getElementById("wrap");
varchilds=content.getElementsByTagName("span");
if(childs.length>0){
content.removeChild(childs[childs.length-1]);
deleteSpan();
}
}

$("#wrap").css({"left":"100px"});
varwrap=document.getElementById("wrap");
wrap.style.left="100px";

$("#banner").hide();
document.getElementById("banner").style.display="none";

$("#banner").show();
document.getElementById("banner").style.display="block";

$("#people").addClass("people_run2");
document.getElementById("people").classList.add("people_run2");

$("#people").removeClass("people_run1");
document.getElementById("people").classList.remove("people_run1");

$("#number").text(1);
document.getElementById("number").innerHTML=1;
$.ajax({
type:"POST",
url:"run.php",
data:"s="+last_step,
dataType:"JSON",
timeout:2000,
success:function(data){
//处理回调
}
});

//1.创建XMLHTTPRequest对象
varxmlhttp;
if(window.XMLHttpRequest){
//IE7+,Firefox,Chrome,Opera,Safari
xmlhttp=newXMLHttpRequest;

//针对某些特定版本的mozillar浏览器的bug进行修正
if(xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType("text/xml");
};
}elseif(window.ActiveXObject){
//IE6,IE5
xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
};

if(xmlhttp.upload){
//2.回调函数
//onreadystatechange是每次readyState属性改变的时候调用的事件句柄函数
xmlhttp.onreadystatechange=function(e){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
varjson=eval("("+xmlhttp.responseText+")");
//处理回调
}
}
};

//3.设置连接信息
//初始化HTTP请求参数,但是并不发送请求。
//第一个参数连接方式,第二是url地址,第三个true是异步连接,默认是异步
//使用post方式发送数据
xmlhttp.open("POST","/run.php",true);

//4.发送数据,开始和服务器进行交互
//发送HTTP请求,使用传递给open()方法的参数,以及传递给该方法的可选请求中如果true,send这句话会立即执行
//如果是false(同步),send会在服务器数据回来才执行
//get方法在send中不需要内容
varformdata=newFormData();
formdata.append("s",last_step);
xmlhttp.send(formdata);
}
$("btn").bind({
"touchstart":function(){
}
});
document.getElementById("btn").ontouchstart=function(){
};