Skip to the content.

你觉得jQuery或zepto源码有哪些写的好的地方

 (function( window, undefined ) {

         //用一个函数域包起来,就是所谓的沙箱

         //在这里边var定义的变量,属于这个函数域内的局部变量,避免污染全局

         //把当前沙箱需要的外部变量通过函数参数引入进来

         //只要保证参数对内提供的接口的一致性,你还可以随意替换传进来的这个参数

        window.jQuery = window.$ = jQuery;

    })( window );

jQuery 的实现原理?

jQuery.fn 的 init 方法返回的 this 指的是什么对象? 为什么要返回 this?

jQuery.extend 与 jQuery.fn.extend 的区别?

$.extend({
  min: function(a, b) { return a < b ? a : b; },
  max: function(a, b) { return a > b ? a : b; }
});
$.min(2,3); //  2
$.max(4,5); //  5

$.fn.extend({
   alertValue: function() {
      $(this).click(function(){
        alert($(this).val());
      });
   }
});

$("#email").alertValue();

jQuery 的属性拷贝(extend)的实现原理是什么,如何实现深拷贝?

jQuery 的队列是如何实现的?队列可以用在哪些地方?

var func1 = function(){alert('事件1');}
var func2 = function(){alert('事件2');}
var func3 = function(){alert('事件3');}
var func4 = function(){alert('事件4');}

// 入栈队列事件
$('#box').queue("queue1", func1);  // push func1 to queue1
$('#box').queue("queue1", func2);  // push func2 to queue1

// 替换队列事件
$('#box').queue("queue1", []);  // delete queue1 with empty array
$('#box').queue("queue1", [func3, func4]);  // replace queue1

// 获取队列事件(返回一个函数数组)
$('#box').queue("queue1");  // [func3(), func4()]

// 出栈队列事件并执行
$('#box').dequeue("queue1"); // return func3 and do func3
$('#box').dequeue("queue1"); // return func4 and do func4

// 清空整个队列
$('#box').clearQueue("queue1"); // delete queue1 with clearQueue

jQuery 中的 bind(), live(), delegate(), on()的区别?

是否知道自定义事件? jQuery 里的 fire 函数是什么意思,什么时候用?

  document.createEvent(type); // 创建事件
  event.initEvent(eventType, canBubble, prevent); // 初始化事件
  target.addEventListener('dataavailable', handler, false); // 监听事件
  target.dispatchEvent(e);  // 触发事件

jQuery 通过哪个方法和 Sizzle 选择器结合的?

jQuery 中如何将数组转化为 JSON 字符串,然后再转化回来?

// 通过原生 JSON.stringify/JSON.parse 扩展 jQuery 实现
 $.array2json = function(array) {
    return JSON.stringify(array);
 }

 $.json2array = function(array) {
    // $.parseJSON(array); // 3.0 开始,已过时
    return JSON.parse(array);
 }

 // 调用
 var json = $.array2json(['a', 'b', 'c']);
 var array = $.json2array(json);

jQuery 一个对象可以同时绑定多个事件,这是如何实现的?

  $("#btn").on("mouseover mouseout", func);

  $("#btn").on({
      mouseover: func1,
      mouseout: func2,
      click: func3
  });

针对 jQuery 的优化方法?

jQuery 的 slideUp 动画,当鼠标快速连续触发, 动画会滞后反复执行,该如何处理呢?

jQuery UI 如何自定义组件?

jQuery 与 jQuery UI、jQuery Mobile 区别?

jQuery 和 Zepto 的区别? 各自的使用场景?