代码以jQuery 1.83 为例

一 :Q: What is the difference between .get()[], and .eq()?

A: eq返回原生jQuery对象,截取某些el元素生成Jquery新对象

 get和[]返回的都是原生的Dom对象,原理一致

 get和[]区别是get是通过jQuery对象的方法获取,[]是根据jQuery是一个数组对象获取

二: What is the difference between .bind().live(), and .delegate()?

A: 它们实质调用的都是jQuery实例对象的on函数,更底层是jQuery.event.add();

官方描述:Attach an event handler function for one or more events to the selected elements

.on( events [, selector ] [, data ], handler(eventObject) )

三种绑定函数代码

bind: this.on(types, null, data, fn); 直接绑定到元素上

live: jQuery(this.context).on(types, this.selector, data, fn); 将事件绑定到context上,冒泡,当触发元素为this.selector时触发

delegate: this.on(types. selector, data, fn)

selector如何添加

三: How, and why, would you namespace a bound event handler?

A: click.myPlugin.simple定义了两个命名空间 为这个独特的click事件 可以被移除通过 .off('click.myPlugin') or .off('click.simple')

命名空间跟css 相似都不是分层的,只需要一个名字来匹配

jquery.event jquery.event.global jquery.event.handle

四:What is the difference between $ and $.fn? Or just what is $.fn.

1
2
3
window.jQuery = window.$ = jQuery;
 
jQuery.fn = jQuery.prototype = {}

五:what's jQuery's context/selector and why use it

1
2
3
<div id="context">
    <div id="inner"></div>
</div>

  

context/selector 示例

1
2
3
4
5
6
7
8
9
<script>
var $ret = $('#inner', $('#context')[0]);
console.log($ret.selector); // #inner
console.log($ret.context); // #context
 
var $ret = $('#inner', '#context');
console.log( $ret.selector); // '#context #inner'
console.log( $ret.context); // document
</script>

context 就是限定查找的范围

context 必须是一个DOM元素,context 底层还是用了.find方法来实现

官方API selector context is implemented with the .find() method,so $("span", this) is equivalent to $(this).find("span")

注:例子仅供展示,id类型查找非常快,所以不要用这种context,直接$('#inner')最好,当查找tag/class时用会比较高效.

六:Difference between 'delegate()' and 'live()'

delegate 指定了委托对象

live委托给了jQuery的context,1.9以后删除了,用on代替

一下三个效果一致

1
2
3
$(selector).live(events, data, handler);
$(document).delegate(selector, events. data, handler);
$(document).on(events, selector, data, handler);

七:What is the effetcs (of fx) queue?

.queue([queueName])

官方API:Show the queue of functions to be executed on the matched elements.

Queues allow a sequence of actions to be called on an element asynchronously, without halting program execution. 最大的特点是这些代码异步执行,不影响后面代码操作,说白了就是将他们放入一个队列中

1
2
3
4
5
6
7
8
9
10
11
12
13
div { margin:3px; width:40px; height:40px;
        position:absolute; left:0px; top:60px;
        background:green; display:none;
 }
 
div.newcolor { background:blue; }
p { color:red; } 
 
<p>The queue length is: <span></span></p>
<div id="box">1</div>
<div style="top:120px;">2</div>
<button id="start">start</button>
<button id="end">end</button>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script>
var $box = $('div');
 
function runIt() {
    $box.show()
        .animate({'left':"+=200"}, 2000)
        .queue(function(next){
            $(this).addClass('newcolor');
            next();
        })
        .slideToggle(1000)
        .slideToggle('fast')
        .animate({'left':"-=200"}, 2000)
        .queue(function(next){
            $(this).removeClass('newcolor');
            next();
        }) 
        .hide('slow')
        .show(200)
        .slideUp("normal");
}
 
function showIt(){
    var n = $box.queue();
    $("span").text(n.length);
    setTimeout(showIt, 100);
}
 
function stopIt(){
    $box.queue('fx', []);
    $box.stop();
}
 
$('#start').click(function(){
    runIt();
});
$('#end').click(function(){
    stopIt();
})
showIt();
 
 
</script>

八:attr和prop的区别  

attr是操作属性节点,DOM的API setAttribute,getAttribute(HTML)

prop是操作获取到的对应js对象的属性 (JS)

场景:遇到要获取或设置checked,selected,readonlydisabled等属性时,用prop方法显然更好

prop更高效,因为attr要DOM访问

附加:

Generally, DOM attributes represent the state of DOM information as retrieved from the document, such as the value attribute in the markup <input type="text" value="abc">. DOM properties represent the dynamic state of the document; for example if the user clicks in the input element above and typesdef the .prop("value") is abcdef but the .attr("value") remains abc.

jQuery常见面试题(转)的更多相关文章

  1. jQuery 常见面试题

    一 :Q: What is the difference between .get(), [], and .eq()? A: eq返回原生jQuery对象,截取某些el元素生成Jquery新对象 ge ...

  2. 【javascript常见面试题】常见前端面试题及答案

    转自:http://www.cnblogs.com/syfwhu/p/4434132.html 前言 本文是在GitHub上看到一个大牛总结的前端常见面试题,很多问题问的都很好,很经典.很有代表性.上 ...

  3. Vue常见面试题汇总

    Vue框架常见面试题   1.active-class是哪个组件的属性?嵌套路由怎么定义? 答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么 ...

  4. java常见面试题及答案 1-10(基础篇)

    java常见面试题及答案 1.什么是Java虚拟机?为什么Java被称作是"平台无关的编程语言"? Java 虚拟机是一个可以执行 Java 字节码的虚拟机进程.Java 源文件被 ...

  5. Web开发的常见面试题HTML和HTML5等

    作为一名前端开发人员,HTML,HTML5以及网站优化都是必须掌握的技术,下面列举一下HTML, HTML5, 网站优化等常见的面试题: HTML常见面试题: 1. 什么是Semantic HTML( ...

  6. 常见面试题之ListView的复用及如何优化

    经常有人问我,作为刚毕业的要去面试,关于安卓开发的问题,技术面试官会经常问哪些问题呢?我想来想去不能一股脑的全写出来,我准备把这些问题单独拿出来写,并详细的分析一下,这样对于初学者是最有帮助的.这次的 ...

  7. iOS常见面试题汇总

    iOS常见面试题汇总 1. 什么是 ARC? (ARC 是为了解决什么问题而诞生的?) ARC 是 Automatic Reference Counting 的缩写, 即自动引用计数. 这是苹果在 i ...

  8. JDBC常见面试题

    以下我是归纳的JDBC知识点图: 图上的知识点都可以在我其他的文章内找到相应内容. JDBC常见面试题 JDBC操作数据库的步骤 ? JDBC操作数据库的步骤 ? 注册数据库驱动. 建立数据库连接. ...

  9. Mybatis常见面试题

    Mybatis常见面试题 #{}和${}的区别是什么? #{}和${}的区别是什么? 在Mybatis中,有两种占位符 #{}解析传递进来的参数数据 ${}对传递进来的参数原样拼接在SQL中 #{}是 ...

随机推荐

  1. 分布式架构--第一篇--项目拆分(maven命令生成多模块项目)

    预览生成的项目结构: ying-yue-parent // 顶级总编译控制模块 ying-yue-lib // jar模块 ying-yue-model // 模型对象模块 ying-yue-dao ...

  2. OpenCms 集成外部Solr Server

    OpenCms默认是以内嵌的Solr作为全文搜索服务的,不过我们也可以配置一个独立的Solr搜索服务器 设置外部Solr Server 1. 从Solr 官方站点http://lucene.apach ...

  3. 【转】eclipse技巧1

    俗话说的好啊,“工于利启事,必先善其器”,如果说你的编程功底是一个枪法的话,那么强大的eclipse就是android战士们最好的武器. 这里,我们来总结eclipse的使用技巧,从而使我们的编程达到 ...

  4. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分

    题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...

  5. Combox 实现百度收索框效果

    标题中所谓百度收缩框效果,就是在输入数据的时候,自动提示,来张图就明白了: 用Combox来实现这个功能只是需要设置三个A开头的属性就OK了:AutoCompleteSource.AutoComple ...

  6. 【BZOJ】【1202】【HNOI2005】狡猾的商人

    Orz iwtwiioi  http://www.cnblogs.com/iwtwiioi/p/3887617.html 并查集+前缀和 啊……这题应该是水题吧?但是我这个大沙茶居然一天都没想出来…… ...

  7. [转载]115个Java面试题和答案

    不知道大家有没有这样的体会,就是找工作的时候不得不准备大量面试题,而工作的时间长了面试题里的精髓却忘的差不多了... 转载几篇Java面试的bolg,温故而知新,最重要的是常来看看. 1. http: ...

  8. VSFTPD全攻略(/etc/vsftpd/vsftpd.conf文件详解)

    /etc/vsftpd/vsftpd.conf文件详解,分好类,方便大家查找与学习 #################匿名权限控制############### anonymous_enable=YE ...

  9. 我的第一款windows phone软件

    我的第一个windows phone应用发布成功了,大家支持下,名字叫吕氏春秋,发布人是我的英文名xmfdsh http://www.windowsphone.com/zh-cn/store/app/ ...

  10. DOS系统功能调用表(INT 21H)

    AH 功能 调用参数 返回参数 00 程序终止(同INT 20H) CS=程序段前缀 01 键盘输入并回显 AL=输入字符 02 显示输出 DL=输出字符 03 异步通迅输入 AL=输入数据 04 异 ...