原生js模拟jquery写法
function $_custom(fun)
{
document.onreadystatechange = function()
{
if (document.readyState == "complete")
{
fun();
}
}
}
function $(val){
if(val.indexOf("#")==0)
{
var ob=new Array();
var obj=document.getElementById(val.substring(1));
ob.push(obj);
custom.call(ob);
return ob;
}
if(val.indexOf(".")==0)
{
var obj=document.getElementsByTagName("*");
var ob=new Array();
for(var x in obj)
{
if(obj[x].className==val.substring(1))
{
obj2=obj[x];
ob.push(obj2);
}
}
custom.call(ob);
return ob;
}
}
var custom=function(){
var actions=["click","blur","focus","mouseout","mouseover","change"];
//样式处理
this.css=function(param){
for(var i = 0;i < this.length;i++)
{
for(var key in param)
{
this[i].style[key]=param[key];
}
}
};
var _this=this;
//函数处理
(function(){
for(var k in actions){
var _o=actions[k];
_this[_o]= function(){
var _oo=_o;
return function(fun)
{
for(var i = 0;i < _this.length;i++)
{
_this[i]["on"+_oo]=function(event)
{
fun(event);
}
}
}
}(_o);
}
})(actions);
//还原javascript基本写法
this.revert=function(){
if(_this.length==1)
{
return _this[0];
}
else{
alert("Does not support!");
console.log("Does not support!");
}
};
//模拟鼠标事件
this.similar=function(actions){
if(document.all) {
_this[0][actions]();
}
else {
var e = document.createEvent("MouseEvents");
e.initMouseEvent(actions, true, true);
_this[0].dispatchEvent(e);
}
};
//绑定事件
this.bind=function(actions,fun){
if(document.all) {
for(var i = 0;i < _this.length;i++)
_this[i].attachEvent("on"+actions,function(){fun.call(_this[i])});
}else{
for(var i = 0;i < _this.length;i++)
_this[i].addEventListener(actions,fun);
}
}
}
使用方法介绍 样式处理 <script type="text/javascript" src="$_custom.js"></script>
<script type="text/javascript">
$_custom(function(){
$("#sp").css({width:"100px",height:"100px",border:"1px solid red"});
$(".sp2").css({width:"100px",height:"100px",border:"1px solid red"});
})</script>
<div id="sp">test</div>
<div class="sp2">test</div>
<div class="sp2">test</div> 事件处理 //支持的事件在actions数组里拓展
$("#sp").click(function(){
alert(′test′);
}) 原始写法 $("#sp").revert().style.display=′none′; 模拟鼠标事件 $("#sp").click(function(){
alert(′clicked′);
})
$("#sp").similar("click");
//这样刚进入网页即运行点击事件了 事件绑定 $("#sp").bind("click",function(){
alert(′you click′);
}) 获取鼠标位置 $("#sp").click(function(){
alert(event.x);
})
//event参数可直接支持调用,可以阻止事件冒泡等,不信试试吧
原生js模拟jquery写法的更多相关文章
- 原生JS模拟jQuery $
模拟jQuery的$选择器 在获取元素的时候使用ID选择器,返回的是一个对象:使用类选择器或者标签选择器返回可能是一组元素:将获取到的一个或一组元素进行一个简易的封装封装成一个TQObject 什么是 ...
- 使用原生js模拟jQuery选择器,实现new方法,兼容ie5
// 考虑到兼容ie5,未使用es6语法 /* 使用方法: 在<head>标签中(需使用ready方法): <script src="./jQuery2.js"& ...
- 原生js模拟jquery中的addClass和removeClass方法
js代码: //添加类 function addClass(obj,className) { if(obj.className == '') { //如果没有class obj.className = ...
- 原生js和jquery实现图片轮播特效
本文给大家分享的是使用原生JS和JQ两种方法分别实现相同的图片轮播特效,十分的实用,也非常方便大家对比学习原生js和jQuery,有需要的小伙伴可以参考下. 1)首先是页面的结构部分对于我这种左右切换 ...
- 原生js和jquery实现图片轮播特效(转)
本文给大家分享的是使用原生JS和JQ两种方法分别实现相同的图片轮播特效,十分的实用,也非常方便大家对比学习原生js和jQuery,有需要的小伙伴可以参考下. 1)首先是页面的结构部分对于我这种左右切换 ...
- onload事件与ready事件的区别,原生js与jquery的区别
onload事件与ready事件分别是原生js与jquery的入口函数 原生js入口函数写法: window.onload=function(){ } jquery入口函数写法: $(document ...
- 原生js、jQuery实现选项卡功能
在大家在网上平常浏览网页的时候,想必各位都会看到选项卡功能,在这里给大家详解一下用原生js.jQuery如何来写一些基本的选项卡 话不多说,先给各位看一下功能图: 好了,下边 ...
- 【JS】怎样用原生JS实现jQuery的ready方法
Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,只是与window.onload方法还是有差别的. 总的来说,window. ...
- 【前端性能】必须要掌握的原生JS实现JQuery
很多时候,我们经常听见有人说jquery有多快多快.在这个各种类库满天飞的时候,不得不说的是,能有原生JS快吗? 是的,明显原生JS要更快,因为诸如JQuery这样的库必须要兼容各种浏览器和低版本和许 ...
随机推荐
- loj6570 毛毛虫计数(生成函数FFT)
link 巨佬olinr的题解 <-- olinr很强 考虑生成函数 考虑直径上点数>=4的毛毛虫的直径,考虑直径中间那些节点以及他上面挂的那些点的EGF \(A(x)=\sum_{i\g ...
- Shell基本知识
Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以使用Shell来启动.挂起.停止甚至编写一些程序. Shell还是一个功 ...
- history.back返回是浏览器错误码:ERR_CACHE_MISS
解决方法: 如果访问的是php文件中添加:header("Cache-control: private"); 如果使用的是模板引擎(tp5):{php}header("C ...
- CDQZ Day5
1DP #1题目名称 题目名称匹配块路径染色输入文件名 输入文件名match.in.in.inblock.inpath.inpaint.in输出文件名 输出文件名match.out.out.out.o ...
- jq扩展获取表单值、设置值
不多说,直接上代码 //jq添加插件 (function (window, $) { $.fn.serializeJson = function () { var serializeObj = {}; ...
- UNIX SHELL基础知识总结(一)
1. Unix常目录结构与作用: 2. 基本命令: $echo $date $who $who am i 3. 创建文件的几种方式: A. touch FileName 创建空文件 B. > ...
- nodejs --- 核心概念
nodejs是2009年有Ryan Dahl利用google的V8引擎打造的基于事件循环实现的异步I/O框架,它选择JavaScript作为开发语言,正是因为V8的性能远超过其他脚本语言.目前expr ...
- Delphi导出word
//导出Wordprocedure TFrm_Computing.ExportWord;varwordApp, WordDoc, WrdSelection, wrdtable, wrdtable1, ...
- android volley 发送 POST 请求
Map<String, String> params = new HashMap<String, String>(); params.put("fromUser&qu ...
- android中The connection to adb is down,问题和解决
android中The connection to adb is down,问题和解决 自己总结的在android中常会出现的不好解决的问题和方法(其中第三个方法经过了四天的折磨.....哎) 1 ...