JavaScript--&&--JQuery功能的代码对比(三)

之前已经列举了大量的使用原生JavaScript替代JQuery的例子,在本文中将继续列举!

1、表单

获取焦点

$('#test').focus();  

$('#test').focus(function(){});



var t = document.getElementById('test');

function addEvent(dom, type, handle, capture) {       

  if(dom.addEventListener) {       

    dom.addEventListener(type, handle, capture);        

  } else if(dom.attachEvent) {       

    dom.attachEvent("on" + type, handle);       

  } 

}; 

function focus(elem, fn) {

  if(fn && typeof fn === 'function') {

    addEvent(elem, 'focus', fn);

  } else {

    elem.focus();

  }

}



focus(t, function(){});


失去焦点

$('#test').blur();

$('#test').blur(function(){});

   

function blur(elem, fn) {    

  if(fn && typeof fn === 'function') {    

    addEvent(elem, 'blur', fn);    

  } else {    

    elem.blur();    

  }   

}    



blur(t, function(){});


实时监控

$('#test').on('input propertychange', fn);



function inputChange(dom, fn, capture) {   

  capture = capture || false;   

  addEvent(dom, 'input', fn, capture);

  addEvent(dom, 'propertychange', fn, capture);   

}



inputChange(t, function(){});



2、判断类型


判断类型

$.type(obj);



Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, '$1').toLowerCase();


判断是否为一个函数

$.isFunction(fn)



function isFunction(fn){

  return typeof fn === 'function';

}


判断是否为数字

$.isNumeric(num);



function isNumber(num) {

  var type = typeof num;

  return ( type === 'number' || type === 'string') && 

      !isNaN( num - parseFloat( num ) );

}


判断是否为数组

$.isArray(obj);



function isArray(obj) {

  if( Array.isArray ) {

    return Array.isArray(obj);

  } else {

    return Object.prototype.toString.call(obj) === '[object Array]';

  }

}



3、时间


获取当前时间

$.now()



new Date().getTime();



/* 更简单 */

+new Date();



4、改变上下文(this)


$.proxy(fn, context);



fn.bind(context);



5、空函数


创建一个空函数

var fn = $.noop();



var fn = function() {}



6、数组


合并数组

$.merge(arr1, arr2)



function (arr1, arr2) {

  return arr1.concat(arr2);

}



类数组对象转换成数组

var divs = document.querySelectorAll('div');



var arr = $.makeArray(divs);



var arr = Array.prototype.slice.call(divs);



// ES6 

var arr = Array.from(divs)



7、Iframe


获取iframe的document

$('#iframe').contents();



var iframe = document.getElementById('iframe');

iframe.contentDocument;



8、元素包含关系


检查一个DOM元素是另一个DOM元素的后代

$.contains(parent, child);



function contains(root, el) { 

  /* Chrome / Firefox */  

  if (root.compareDocumentPosition) {  

    return root === el || !!(root.compareDocumentPosition(el) & 16);  

  } 

  /* IE */

  if (root.contains && el.nodeType === 1){   

    return root.contains(el) && root !== el;   

  }   

  while ((el = el.parentNode)) {  

    if (el === root) { return true; }  

    return false;   

  }

}



9、scroll


设置/获取window滚动位置

/*获取*/

$(window).scrollTop();



(document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop



/*设置*/

$(window).scrollTop(10);



(document.documentElement && document.documentElement.scrollTop = 10) || document.body.scrollTop = 10;


设置某个元素滚动位置

$('#test').scrollTop(10);



var t = document.getElementById('test');



t.scrollTop = 10;

注意:别加单位!



10、节点


获取元素的最近的祖先定位(position非static)元素

$('#test').offsetParent();



var t = document.getElementById('test');

t.offsetParent;



如有错误,欢迎指正!如有更好建议,欢迎留言!

著作权归作者所有。

商业转载请联系作者获得授权,非商业转载请注明出处。http://ghmagical.com/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值