17个基本的jQuery面试问题 *

最好的jQuery开发人员和工程师可以回答的全部基本问题. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

现在就聘请一名顶级jQuery开发人员
Toptal标志is an exclusive network of the top freelance software developers, 设计师, 金融专家, 产品经理, 和世界上的项目经理. Top companies hire Toptal freelancers for their most important projects.

面试问题

1.
< div >

解释下面的代码将做什么:

$("div#第一个, div.第一个, ol#items > [名字$='第一个']" )
查看答案
< div >

这段代码执行一个查询来检索任何

元素 id 第一个, + 所有
元素 class 第一个, + 元素的子元素
    元素和其 名字 属性以字符串结束 “第一”. This is an example of using multiple selectors at once. 该函数将返回一个包含查询结果的jQuery对象.

2.
< div >

下面的代码适用于需要为页面上的所有按钮定义单击处理程序的应用程序, including those buttons that may be added later dynamic所有y.

这段代码有什么问题, 以及如何将其固定为正常工作,即使是稍后动态添加的按钮?

// define the click h和ler for 所有 buttons
$("button").Bind ("click", 函数() {
    按钮被点击!" )
});

/* ... 一段时间后 ... */

// dynamic所有y add a不her button to the page
$("html").append( "" );
查看答案
< div >

的 button that is added dynamic所有y after the c所有 to bind () 有点击处理程序附加吗. 这是因为 bind () 方法只将处理程序附加到调用时存在的元素 bind () 是由.

这个问题通过使用“事件冒泡”来匹配当前和未来元素上的事件的函数来解决. 在过去,这是通过替换来完成的 bind ()生活(). 生活() 在jQuery 1.7虽然. 委托() 类似于 生活() but also provides control over how far an 事件 must bubble up the DOM.

但是,推荐的方法是使用 on(),它可以表现为 bind (), 生活(), or 委托() 取决于语法. 下面的代码将处理程序附加到所有当前和未来的按钮:

// define the click h和ler for 所有 buttons
$(文件).On ("click", "button", 函数() {
    按钮被点击!" )
});

/* ... 一段时间后 ... */

// dynamic所有y add a不her button to the page
$("html").append( "" );
3.
< div >

What selector would I use to query for 所有 elements 与 an ID that 结束 用一个特定的字符串? Also, how would I modify the selector to retrieve 只有

elements whose IDs end 与 that same string? 提供一个示例.

查看答案
< div >

Let’s say you want to retrieve 所有 elements whose IDs end 与 “txtTitle”. This could be done using the following selector:

$(“[id = ' txtTitle ']美元”)

只能取回

elements whose IDs end 与 “txtTitle”, the selector would be:

$ (" div [id = ' txtTitle ']美元”)

申请加入Toptal的发展网络

并享受可靠、稳定、远程 自由职业者jQuery开发人员职位

申请成为自由职业者
4.
< div >

这是怎么回事 $ 在jQuery? 它是什么,它意味着什么?

此外,如何将jQuery与另一个JavaScript库结合使用 $ 为命名? 如果你能给出两个答案,那就加分了.

查看答案
< div >

$ 在JavaScript中没有特殊的含义,它可以自由地用于对象命名. In jQuery, it is simply used as an alias for the jQuery 对象和 jQuery () 函数.

然而,jQuery并没有垄断 $, 所以你可能会遇到这样的情况,你想使用它与另一个JS库也使用 $, which would therefore result in a naming conflict. jQuery提供了 jQuery.noConflict () 方法正是出于这个原因. C所有ing this 方法 makes it necessary to use the underlying 名字 jQuery instead in subequent references to jQuery 和 its 函数s.

Here’s an example from the jQuery 文档ation:




Alternatively, you can also use a closure instead of the $.noConflict () 方法; e.g.:

(函数 ($) {
  // Code in which we know exactly what the meaning of $ is
} (jQuery)); 
5.
< div >

给定以下HTML:

和以下CSS:

div#扩张器{
  宽度:100 px;
  身高:100 px;
  背景颜色:蓝色;
}

编写代码在jQuery动画 #扩张器 Div,在三秒钟内将其从100 x 100像素扩展到200 x 200像素.

查看答案
< div >

这可以在jQuery中这样做:

$("#扩张器").动画(
  {
    宽度:“200 px”,
    高度:“200 px”,
  },
  3000 );
6.
< div >

什么是jQuery中的方法链? 提供一个示例.

它有什么优点?

查看答案
< div >

方法链接是jQuery的一个特性,它允许在单个代码语句中按顺序对jQuery选择执行多个方法. For example, the following snippets of code are equivalent:

没有链接:

$("button#play-movie").on(“点击”,播放电影);
$("button#play-movie").Css ("background-color", "orange");
$("button#play-movie").显示();

链接:

$("button#play-movie").开启(“点击”,播放影片)
                        .Css ("background-color", "orange")
                        .显示();

注意到链接, the button 只有 needs to be selected one time, 如果没有链条, jQuery必须在应用每个方法之前搜索整个DOM并找到按钮. 因此, 除了产生更简洁的代码, jQuery中的方法链提供了潜在的强大性能优势.

注意: 准确地说,应该注意的是,jQuery中的方法链接不是 只有 way to avoid repetitively searching the entire DOM. One could also set a variable equal to the initial DOM search results (i.e., in the above example, one could set a variable equal to $("按钮#演电影” 然后调用 on(), css (), 显示() 该变量的方法). 但也就是说, 链接仍然是更简洁和高效的选择,并且不需要将结果缓存到局部变量中.

7.
< div >

解释一下下面代码的作用:

$("div").Css ("width", "300px").添加("p").Css ("background-color", "blue");
查看答案
< div >

This code uses 方法 chaining to accomplish a couple of things. 首先,它选择所有的

elements 和 changes their CSS width to 300px. 然后,它把所有的

元素的当前选择,所以它最终可以改变CSS背景颜色

元素变为蓝色.

8.
< div >

两者的区别是什么 jQuery.get ()jQuery.ajax ()?

查看答案
< div >

jQuery.ajax () is the 所有-encompassing Ajax request 方法 provided by jQuery. It 所有ows for the creation of highly-customized Ajax requests, 与 options for how long to wait for a response, 如何处理失败, 请求是阻塞(同步)还是非阻塞(异步), 请求什么格式的响应, 还有更多的选择.

jQuery.get () 是使用快捷方法吗 jQuery.ajax () 创建一个Ajax请求,该请求通常用于简单的信息检索. Other pre-built Ajax requests are provided by jQuery, such as jQuery.post (), jQuery.getScript (), jQuery.getJSON ().

9.
< div >

Which of the two lines of code below is more efficient? 解释你的答案.

文档.getElementById("logo");

or

$("#logo");
查看答案
< div >

第一行代码是没有jQuery的纯JavaScript,效率更高,速度更快. 执行第二行代码(即jQuery)将触发对JavaScript版本的调用.

jQuery构建在JavaScript之上,并使用底层的方法使DOM操作更容易, 以一些性能开销为代价. 最好记住,jQuery并不总是比普通的JavaScript更好. 始终考虑使用jQuery是否真的为您的项目提供了有用的优势.

10.
< div >

解释并对比……的用法 事件.pr事件Default ()事件.stopPropagation (). 提供一个示例.

查看答案
< div >

事件.stopPropagation () stops an 事件 from bubbling up the 事件 chain, whereas 事件.pr事件Default () 只有 precludes the browser’s default action on that 事件 from occurring, but the 事件 still propogates up the 事件 chain.

For example, consider the following code snippet:

// in this example, 'foo' is a div containing button 'bar'

$ (" # foo ").点击(函数(){
   //鼠标点击div 'foo'
});

$ (" # ").点击(函数(e) {
   //鼠标点击按钮'bar'
   e.stopPropagation ();
});

由于c所有 to stopPropagation () 在按钮的click处理程序中, the 事件 never propogates to the div so its click h和ler never fires. 它有效地阻止父元素了解其子元素上的事件.

In contrast, if you replaced the above c所有 to stopPropagation () 呼唤着 pr事件Default (),只有浏览器的默认操作会被排除,但是div的click处理程序仍然会被触发.

(注:虽然 stopPropagation ()pr事件Default () 方法s are mostly used 在jQuery 事件 h和ling implementations, 它们也适用于纯JavaScript.)

11.
< div >

返回完成了什么 从(a) jQuery事件处理程序, (b) a regular JavaScript onclick 事件 h和ler for an anchor tag, (c) a regular JavaScript onclick 事件 h和ler for a non-anchor tag (e.g., div,按钮等.)?

查看答案
< div >

(a)从jQuery事件处理程序返回假实际上与调用相同 这两个 pr事件Default () stopPropagation () 在传递的jQuery事件对象上.

(b)从锚标记的常规JavaScript onclick事件处理程序返回假会阻止浏览器导航到链接地址 stops the 事件 from propagating through the DOM.

(c)从非锚标记的常规JavaScript onclick事件处理程序返回假.g., div,按钮等.)完全没有效果.

12.
< div >

jQuery提供了一个有用的 .克隆() 方法 to create a deep copy of matching elements.

回答以下问题:

  1. 什么是“深度复制”??
  2. 正常情况下是什么 包含在克隆副本中? How can some of this behavior be controlled?
  3. What is a potential “gotcha” when using the .克隆() 方法? (HINT: What is an element attribute that you would gener所有y 想要克隆吗?)
查看答案
< div >

1. 什么是“深度复制”??

.克隆() 方法 performs a deep copy of the set of matched elements, 这意味着它复制匹配的元素 as well as their descendant elements 和 text nodes.

2. 正常情况下是什么 包含在克隆副本中? How can some of this behavior be controlled?

正常情况下:

  • 元素数据中的对象和数组不会被复制,并将继续在克隆元素和原始元素之间共享. To deep copy 所有 data, you must copy each one “manu所有y”.
  • 绑定到原始元素的任何事件处理程序都不会复制到克隆元素.

设置可选的 与DataAndEvents 参数 真正的 生成所有事件处理程序的副本,并将其绑定到元素的新副本.

从jQuery 1开始.4、所有元素数据(附) .数据() 方法)也被复制到新的副本.

从jQuery 1开始.5, 与DataAndEvents 可以选择性地增强 deepWithDataAndEvents to copy the 事件s 和 data for 所有 children of the cloned element.

3. What is a potential “gotcha” when using the 克隆() 方法? (HINT: What is an element attribute that you would gener所有y 想要克隆吗?)

使用 .克隆() 产生重复元素是否有潜在的副作用 id 属性,它们应该是唯一的. 因此,克隆带有。的元素时 id attribute, it’s important to remember to modify the id of the clone, before inserting it into the DOM.

13.
< div >

解释 .承诺() 方法 在jQuery, including how 和 why it would be used.

考虑下面的代码片段. 如果有5个

元素,开始和结束时间之间的差异将显示在页面上?

函数getMinsSecs() {
  var dt = new Date();
  返回dt.getMinutes() +“:”+ dt.getSeconds ();
}

$("input").On ("click"), 函数() {
  $( "p" ).append( "Start time: " + getMinsSecs() + "
" ); $("div").函数(I) { $(this).fdeout (1000 * (i * 2)); }); $("div").承诺().完成(函数(){ $( "p" ).append( "End time: " + getMinsSecs() + "
" ); }); });
查看答案
< div >

.承诺() 方法返回一个动态生成的Promise,一旦将特定类型的所有操作绑定到集合,该Promise就会被解析, 是否排队, 已经结束.

它接受两个可选参数:

  • 类型 -默认, 类型为“fx”, 这意味着当所选元素的所有动画完成时,返回的Promise将被解析.
  • 目标 —如果指定了目标对象, .承诺() 将 attach to it 和 then return this object rather than create a new one.

在提供的代码示例中, 显示的开始和结束时间之间的差异将为10秒. 这是因为 .承诺() 我会等待一切

动画(在这种情况下,所有 渐隐() c所有s) to complete, the last of which 将 complete 10 seconds (i.e.(5 * 2秒).

14.
< div >

什么是正确的方式在jQuery中删除一个元素从DOM之前,它的承诺是解决?

查看答案
< div >

jQuery中返回的Promise被链接到 延迟的对象 存储在 .数据() 对于一个元素. 自 .remove () 方法 removes the element’s data as well as the element itself, it 将 pr事件 any of the element’s unresolved Promises from resolving.

因此,如果需要在解析Promise之前从DOM中删除一个元素,使用 .分离() 取而代之的是 .removeData () 决议后.

15.
< div >

解释两者之间的区别 .分离().remove () jQuery中的方法.

查看答案
< div >

.分离().remove () 方法是一样的,除了 .分离() retains 所有 jQuery data associated 与 the removed elements 和 .remove () 不. .分离() 因此,当删除的元素可能需要在以后的时间重新插入到DOM中是有用的.

16.
< div >

两者有什么区别 文档.准备好()窗口.onload ()?

查看答案
< div >

文档.准备好() 事件 occurs when 所有 HTML 文档s have been loaded, but 窗口.onload () occurs when 所有 content (including images) has been loaded. 所以一般来说 文档.准备好() 事件首先触发.

17.
< div >

两者有什么区别 支持()attr ()?

查看答案
< div >

这两个 支持()attr () 用于获取或设置元素属性的指定属性的值,但是 attr () returns the default value of a property whereas 支持() 返回当前值。.

< div >

的re is more to interviewing than tricky technical questions, 所以这些只是作为一个指南. Not every “A” c和idate worth hiring 将 be able to answer them 所有, nor does answering them 所有 guarantee an “A” c和idate. 一天结束的时候, hiring remains an art, a science — 和 a lot of work.

为什么Toptal

厌倦了面试候选人? 不知道该问什么才能让你得到一份好工作?

让Toptal为你找到最合适的人.

现在就聘请一名顶级jQuery开发人员

我们的独家网络jQuery开发人员

Looking to l和 a job as a jQuery Developer?

让Toptal为你找到合适的工作.

申请成为jQuery开发人员

工作机会从我们的网络

提出面试问题

Submitted questions 和 answers are subject to review 和 editing, 并可能会或可能不会选择张贴, 由Toptal全权决定, 有限责任公司.

*所有字段均为必填项

寻找jQuery开发人员?

寻找 jQuery开发人员? 查看Toptal的jQuery开发人员.

< div >

安妮·亚当斯

自由jQuery开发人员
联合王国Toptal成员2015年9月4日

Anne是一名经验丰富的开发人员,曾在大公司和初创公司工作过. 他在美林(Merrill Lynch)做了8年的工程师,开发了金融交易应用程序, 安妮创立并建立了LoudUp, a music-based social network that she designed, 发达, 从地面向上发射. 她擅长于 .。网技术和JavaScript.

显示更多
< div >

克里斯Sellek

自由jQuery开发人员
美国Toptal成员2022年2月28日

Chris has been in the software engineering industry for over ten years. He started as a QA 和 transitioned into web engineering. He specializes in front-end development 与 反应, 打印稿, 下一个.js. 他已经部署了新的应用程序, 创建了新的流程和CI/CD管道, 指导初级开发人员, exp和ed on machine learning projects using TensorFlow to train models, 以及重构的自动化测试套件. Chris喜欢用代码解决问题.

显示更多
< div >

巴赫Ly

自由jQuery开发人员
澳大利亚Toptal成员2022年5月6日

巴赫的编码和设计都非常完美, 响应, 健壮的, 和可重用的反应应用程序, UI组件库, 无头的网站, 登陆页. 他的二合一技能得到了许多来自纳斯达克上市公司和asx上市公司的高级全栈开发人员的认可. 他帮助开发人员和UX设计师加速了无可挑剔的交付, automatic所有y-tested, 一致的, 快, 和可维护的前端应用程序和库与他的技能互补的混合.

显示更多

Toptal连接 排名前3% 世界各地的自由职业人才.

加入Toptal社区.

了解更多
\n\n\n\n\n

Alternatively, you can also use a closure instead of the $.noConflict () 方法; e.g.:

\n\n
(函数 ($) {\n  // Code in which we know exactly what the meaning of $ is\n} (jQuery)); \n
\n","identifier":3280068},{"questionBody":"

给定以下HTML:

\n\n
\n
\n\n

和以下CSS:

\n\n
div#扩张器{\n  宽度:100 px;\n  身高:100 px;\n  背景颜色:蓝色;\n}\n
\n\n

编写代码在jQuery动画 #扩张器 Div,在三秒钟内将其从100 x 100像素扩展到200 x 200像素.

\n","answerBody":"

这可以在jQuery中这样做:

\n\n
$( \"#扩张器\" ).动画(\n  {\n    width: \"200px\",\n    height: \"200px\",\n  },\n  3000 );\n
\n","identifier":3280069},{"questionBody":"

什么是jQuery中的方法链? 提供一个示例.

\n\n

它有什么优点?

\n","answerBody":"

方法链接是jQuery的一个特性,它允许在单个代码语句中按顺序对jQuery选择执行多个方法. For example, the following snippets of code are equivalent:

\n\n

没有链接:

\n\n
$( \"button#play-movie\" ).on( \"click\", playMovie );\n$( \"button#play-movie\" ).css( \"background-color\", \"orange\" );\n$( \"button#play-movie\" ).显示();\n
\n\n

链接:

\n\n
$( \"button#play-movie\" ).on( \"click\", playMovie )\n                        .css( \"background-color\", \"orange\" )\n                        .显示();\n
\n\n

注意到链接, the button 只有 needs to be selected one time, 如果没有链条, jQuery必须在应用每个方法之前搜索整个DOM并找到按钮. 因此, 除了产生更简洁的代码, jQuery中的方法链提供了潜在的强大性能优势.

\n\n

注意: 准确地说,应该注意的是,jQuery中的方法链接不是 只有 way to avoid repetitively searching the entire DOM. One could also set a variable equal to the initial DOM search results (i.e., in the above example, one could set a variable equal to $( \"button#play-movie\" 然后调用 on(), css (), 显示() 该变量的方法). 但也就是说, 链接仍然是更简洁和高效的选择,并且不需要将结果缓存到局部变量中.

\n","identifier":3280070},{"questionBody":"

解释一下下面代码的作用:

\n\n
$( \"div\" ).css( \"width\", \"300px\" ).add( \"p\" ).css( \"background-color\", \"blue\" );\n
\n","answerBody":"

This code uses 方法 chaining to accomplish a couple of things. 首先,它选择所有的

elements 和 changes their CSS width to 300px. 然后,它把所有的

元素的当前选择,所以它最终可以改变CSS背景颜色

元素变为蓝色.

\n","identifier":3280071},{"questionBody":"

两者的区别是什么 jQuery.get ()jQuery.ajax ()?

\n","answerBody":"

jQuery.ajax () is the 所有-encompassing Ajax request 方法 provided by jQuery. It 所有ows for the creation of highly-customized Ajax requests, 与 options for how long to wait for a response, 如何处理失败, 请求是阻塞(同步)还是非阻塞(异步), 请求什么格式的响应, 还有更多的选择.

\n\n

jQuery.get () 是使用快捷方法吗 jQuery.ajax () 创建一个Ajax请求,该请求通常用于简单的信息检索. Other pre-built Ajax requests are provided by jQuery, such as jQuery.post (), jQuery.getScript (), jQuery.getJSON ().

\n","identifier":3280072},{"questionBody":"

Which of the two lines of code below is more efficient? 解释你的答案.

\n\n
文档.getElementById( \"logo\" );\n
\n\n

or

\n\n
$( \"#logo\" );\n
\n","answerBody":"

第一行代码是没有jQuery的纯JavaScript,效率更高,速度更快. 执行第二行代码(即jQuery)将触发对JavaScript版本的调用.

\n\n

jQuery构建在JavaScript之上,并使用底层的方法使DOM操作更容易, 以一些性能开销为代价. 最好记住,jQuery并不总是比普通的JavaScript更好. 始终考虑使用jQuery是否真的为您的项目提供了有用的优势.

\n","identifier":3280073},{"questionBody":"

解释并对比……的用法 事件.pr事件Default ()事件.stopPropagation (). 提供一个示例.

\n","answerBody":"

事件.stopPropagation () stops an 事件 from bubbling up the 事件 chain, whereas 事件.pr事件Default () 只有 precludes the browser’s default action on that 事件 from occurring, but the 事件 still propogates up the 事件 chain.

\n\n

For example, consider the following code snippet:

\n\n
// in this example, 'foo' is a div containing button 'bar'\n\n$(\"#foo\").点击(函数(){\n   //鼠标点击div 'foo'\n});\n\n$(\"#bar\").点击(函数(e) {\n   //鼠标点击按钮'bar'\n   e.stopPropagation ();\n});\n
\n\n

由于c所有 to stopPropagation () 在按钮的click处理程序中, the 事件 never propogates to the div so its click h和ler never fires. 它有效地阻止父元素了解其子元素上的事件.

\n\n

In contrast, if you replaced the above c所有 to stopPropagation () 呼唤着 pr事件Default (),只有浏览器的默认操作会被排除,但是div的click处理程序仍然会被触发.

\n\n

(注:虽然 stopPropagation ()pr事件Default () 方法s are mostly used 在jQuery 事件 h和ling implementations, 它们也适用于纯JavaScript.)

\n","identifier":3280074},{"questionBody":"

返回完成了什么 从(a) jQuery事件处理程序, (b) a regular JavaScript onclick 事件 h和ler for an anchor tag, (c) a regular JavaScript onclick 事件 h和ler for a non-anchor tag (e.g., div,按钮等.)?

\n","answerBody":"

(a)从jQuery事件处理程序返回假实际上与调用相同 这两个 pr事件Default () stopPropagation () 在传递的jQuery事件对象上.

\n\n

(b)从锚标记的常规JavaScript onclick事件处理程序返回假会阻止浏览器导航到链接地址 stops the 事件 from propagating through the DOM.

\n\n

(c)从非锚标记的常规JavaScript onclick事件处理程序返回假.g., div,按钮等.)完全没有效果.

\n","identifier":3280075},{"questionBody":"

jQuery提供了一个有用的 .克隆() 方法 to create a deep copy of matching elements.

\n\n

回答以下问题:

\n\n
    \n
  1. 什么是“深度复制”??
  2. \n
  3. 正常情况下是什么 包含在克隆副本中? How can some of this behavior be controlled?
  4. \n
  5. What is a potential “gotcha” when using the .克隆() 方法? (HINT: What is an element attribute that you would gener所有y 想要克隆吗?)
  6. \n
\n","answerBody":"
\n

1. 什么是“深度复制”??

\n
\n\n

.克隆() 方法 performs a deep copy of the set of matched elements, 这意味着它复制匹配的元素 as well as their descendant elements 和 text nodes.

\n\n
\n

2. 正常情况下是什么 包含在克隆副本中? How can some of this behavior be controlled?

\n
\n\n

正常情况下:

\n\n
    \n
  • 元素数据中的对象和数组不会被复制,并将继续在克隆元素和原始元素之间共享. To deep copy 所有 data, you must copy each one “manu所有y”.
  • \n
  • 绑定到原始元素的任何事件处理程序都不会复制到克隆元素.
  • \n
\n\n

设置可选的 与DataAndEvents 参数 真正的 生成所有事件处理程序的副本,并将其绑定到元素的新副本.

\n\n

从jQuery 1开始.4、所有元素数据(附) .数据() 方法)也被复制到新的副本.

\n\n

从jQuery 1开始.5, 与DataAndEvents 可以选择性地增强 deepWithDataAndEvents to copy the 事件s 和 data for 所有 children of the cloned element.

\n\n
\n

3. What is a potential “gotcha” when using the 克隆() 方法? (HINT: What is an element attribute that you would gener所有y 想要克隆吗?)

\n
\n\n

使用 .克隆() 产生重复元素是否有潜在的副作用 id 属性,它们应该是唯一的. 因此,克隆带有。的元素时 id attribute, it’s important to remember to modify the id of the clone, before inserting it into the DOM.

\n","identifier":3280076},{"questionBody":"

解释 .承诺() 方法 在jQuery, including how 和 why it would be used.

\n\n

考虑下面的代码片段. 如果有5个

元素,开始和结束时间之间的差异将显示在页面上?

\n\n
函数getMinsSecs() {\n  var dt = new Date();\n  返回dt.getMinutes()+\":\"+dt.getSeconds ();\n}\n\n$( \"input\" ).on( \"click\", 函数() {\n  $( \"p\" ).append( \"Start time: \" + getMinsSecs() + \"
\" );\n $( \"div\" ).函数(I) {\n $(this).fdeout (1000 * (i * 2));\n });\n $( \"div\" ).承诺().完成(函数(){\n $( \"p\" ).append( \"End time: \" + getMinsSecs() + \"
\" );\n });\n});\n
\n","answerBody":"

.承诺() 方法返回一个动态生成的Promise,一旦将特定类型的所有操作绑定到集合,该Promise就会被解析, 是否排队, 已经结束.

\n\n

它接受两个可选参数:

\n\n
    \n
  • \n类型 -默认, 类型为“fx”, 这意味着当所选元素的所有动画完成时,返回的Promise将被解析.
  • \n
  • \n目标 —如果指定了目标对象, .承诺() 将 attach to it 和 then return this object rather than create a new one.
  • \n
\n\n

在提供的代码示例中, 显示的开始和结束时间之间的差异将为10秒. 这是因为 .承诺() 我会等待一切

动画(在这种情况下,所有 渐隐() c所有s) to complete, the last of which 将 complete 10 seconds (i.e.(5 * 2秒).

\n","identifier":3280077},{"questionBody":"

什么是正确的方式在jQuery中删除一个元素从DOM之前,它的承诺是解决?

\n","answerBody":"

jQuery中返回的Promise被链接到 延迟的对象 存储在 .数据() 对于一个元素. 自 .remove () 方法 removes the element’s data as well as the element itself, it 将 pr事件 any of the element’s unresolved Promises from resolving.

\n\n

因此,如果需要在解析Promise之前从DOM中删除一个元素,使用 .分离() 取而代之的是 .removeData () 决议后.

\n","identifier":3280078},{"questionBody":"

解释两者之间的区别 .分离().remove () jQuery中的方法.

\n","answerBody":"

.分离().remove () 方法是一样的,除了 .分离() retains 所有 jQuery data associated 与 the removed elements 和 .remove () 不. .分离() 因此,当删除的元素可能需要在以后的时间重新插入到DOM中是有用的.

\n","identifier":3280079},{"questionBody":"

两者有什么区别 文档.准备好()窗口.onload ()?

\n","answerBody":"

文档.准备好() 事件 occurs when 所有 HTML 文档s have been loaded, but 窗口.onload () occurs when 所有 content (including images) has been loaded. 所以一般来说 文档.准备好() 事件首先触发.

\n","identifier":3280080},{"questionBody":"

两者有什么区别 支持()attr ()?

\n","answerBody":"

这两个 支持()attr () 用于获取或设置元素属性的指定属性的值,但是 attr () returns the default value of a property whereas 支持() 返回当前值。.

\n","identifier":3280081}],"publicUrl":"http://zemw3s2.yutb.net/jquery/interview-questions","tabsSection":{"selectedTab":"client","items":[{"类型":"client","label":"我的招聘","activeIconUrl":"http://bs-uploads.toptal.io/blackfish-uploads/components/interview_questions_page/tabs_section/item/4917097/active_icon/optimized/ICON_TALENT__ACTIVE__2x-64d6654dfad0c04528a0525c494b144f.png","inactiveIconUrl":"http://bs-uploads.toptal.io/blackfish-uploads/components/interview_questions_page/tabs_section/item/4917097/inactive_icon/optimized/ICON_TALENT__NON_ACTIVE__2x-8710ec2d9e5d5ba4f8f684771a271d5b.png"},{"类型":"talent","label":"我在找工作","activeIconUrl":"http://bs-uploads.toptal.io/blackfish-uploads/components/interview_questions_page/tabs_section/item/4917098/active_icon/optimized/ICON_CLIENT__ACTIVE__2x-9b251ec42929b7b96f7e20473b501eae.png","inactiveIconUrl":"http://bs-uploads.toptal.io/blackfish-uploads/components/interview_questions_page/tabs_section/item/4917098/inactive_icon/optimized/ICON_CLIENT__NON_ACTIVE__2x-de363650453eabe3d2c4e70e75deaaad.png"}]},"heroSection":{"title":"17个基本的jQuery面试问题","subtitle":"最好的jQuery开发人员和工程师可以回答的全部基本问题. Driven from our community, we encourage experts to submit questions 和 offer feedback.","cta":{"label":"Submit an Interview Question","href":"#submit-question"},"ctas":{"client":{"label":"现在就聘请一名顶级jQuery开发人员","href":"http://zemw3s2.yutb.net/hire?interested_in=developers&skill=jquery&skill_job_title=jQueryplusDeveloper"},"talent":{"label":"申请成为jQuery开发人员","href":"http://zemw3s2.yutb.net/talent/apply?as=developers"}},"eatBadgeSection":{"iconUrl":"http://bs-uploads.toptal.io/blackfish-uploads/components/eat_badge_section/5876824/icon/optimized/toptal_logo_white_mono-df3f70ab3f3d7bf2e75fbe7a95401562.svg","label":"is an exclusive network of the top freelance software developers, 设计师, 金融专家, 产品经理, 和世界上的项目经理. Top companies hire Toptal freelancers for their most important projects."}},"authorSection":null,"featuredContributors":null,"questionsSection":{"does note":"的re is more to interviewing than tricky technical questions, 所以这些只是作为一个指南. Not every “A” c和idate worth hiring 将 be able to answer them 所有, nor does answering them 所有 guarantee an “A” c和idate. 一天结束的时候, hiring remains an art, a science — 和 a lot of work.","banner":{"title":"申请加入Toptal的发展网络","subtitle":"并享受可靠、稳定、远程 自由jQuery开发人员 Jobs","cta":{"label":"申请成为自由职业者","href":"/talent/apply","gaCategory":"cta","gaEvent":"cta - interview_questions_block","gaLabel":"Apply to 自由jQuery开发人员 Jobs"}}},"successStorySection":null}> < /脚本