Home > Web Front-end > JS Tutorial > body text

Common jQuery interview questions

零到壹度
Release: 2018-03-23 10:53:06
Original
3228 people have browsed it

This article mainly shares with you common jQuery interview questions. Friends who want to participate in the interview can learn from them. I hope it can help everyone.

1. What is $() in jQuery library? (Answer below) The

$() function is another name for the jQuery() function, which seems weird at first glance and makes jQuery code obscure. Once you get used to it, you'll love its simplicity. The $() function is used to wrap any object into a jQuery object. You are then allowed to call multiple different methods defined on the jQuery object. You can even pass a selector string into the $() function and it will return a jQuery object containing an array of all matching DOM elements. I've seen this question asked a few times, and even though it's so basic, it's often used to differentiate whether a developer knows jQuery or not.

2. There are 5

elements on the web page, how to use jQuery to select them? (Answer)

Another important jQuery question is selector based. jQuery supports different types of selectors, such as ID selectors, class selectors, and tag selectors. Since the question doesn't mention ID and class, you can use the tag selector to select all p elements. jQuery code: $("p"), which returns a jQuery object containing all 5 p tags. For a more detailed answer, see the article linked above.

3. What is the difference between the ID selector and the class selector in jQuery? (Answer)

If you have used CSS, you may know the difference between ID selectors and class selectors, and the same is true for jQuery. ID selectors use IDs to select elements, such as #element1, while class selectors use CSS classes to select elements. Use the ID selector when you only need to select one element, and the class selector if you want to select a group of elements with the same CSS class. During the interview process, there is a high chance that you will be asked to write code using ID selectors and class selectors. The following jQuery code uses the ID selector and the class selector:

<span style="font-size: 14px;">$(&#39;#LoginTextBox&#39;)  // Returns element wrapped as jQuery object with id=&#39;LoginTextBox&#39;<br/>$(&#39;.active&#39;) // Returns all elements with CSS class active.<br/></span>
Copy after login

As you can see, from a syntax perspective, there is another difference between the ID selector and the class selector. The difference is that the former uses the character "#" and the latter uses the character ".". See the answer link above for a more detailed analysis and discussion.

4. 如何在点击一个按钮时使用 jQuery 隐藏一个图片?

这是一个事件处理问题。jQuery为按钮点击之类的事件提供了很好的支持。你可以通过以下代码去隐藏一个通过ID或class定位到的图片。你需要知道如何为按钮设置事件并执行hide() 方法,代码如下所示:

<span style="font-size: 14px;">$(&#39;#ButtonToClick&#39;).click(function(){<br/>    $(&#39;#ImageToHide&#39;).hide();<br/>});<br/></span>
Copy after login

我喜欢这个问题,因为很贴近实际使用,代码也不复杂。

5. $(document).ready() 是个什么函数?为什么要用它?(答案)

这个问题很重要,并且常常被问到。 ready() 函数用于在文档进入ready状态时执行代码。当DOM 完全加载(例如HTML被完全解析DOM树构建完成时),jQuery允许你执行代码。使用$(document).ready()的最大好处在于它适用于所有浏览器,jQuery帮你解决了跨浏览器的难题。需要进一步了解的用户可以点击 answer链接查看详细讨论。

6. JavaScript window.onload 事件和 jQuery ready 函数有何不同?(答案)

这个问答是紧接着上一个的。JavaScript window.onload 事件和 jQuery ready 函数之间的主要区别是,前者除了要等待 DOM 被创建还要等到包括大型图片、音频、视频在内的所有外部资源都完全加载。如果加载图片和媒体内容花费了大量时间,用户就会感受到定义在 window.onload 事件上的代码在执行时有明显的延迟。

另一方面,jQuery ready() 函数只需对 DOM 树的等待,而无需对图像或外部资源加载的等待,从而执行起来更快。使用 jQuery $(document).ready() 的另一个优势是你可以在网页里多次使用它,浏览器会按它们在 HTML 页面里出现的顺序执行它们,相反对于 onload 技术而言,只能在单一函数里使用。鉴于这个好处,用 jQuery ready() 函数比用 JavaScript window.onload 事件要更好些。

7. 如何找到所有 HTML select 标签的选中项?(答案如下)

这是面试里比较棘手的 jQuery 问题之一。这是个基础的问题,但是别期望每个 jQuery 初学者都知道它。你能用下面的 jQuery 选择器获取所有具备 multiple=true 的 标签。

8. jQuery 里的 each() 是什么函数?你是如何使用它的?(答案如下)

each() 函数就像是 Java 里的一个 Iterator,它允许你遍历一个元素集合。你可以传一个函数给 each() 方法,被调用的 jQuery 对象会在其每个元素上执行传入的函数。有时这个问题会紧接着上面一个问题,举个例子,如何在 alert 框里显示所有选中项。我们可以用上面的选择器代码找出所有选中项,然后我们在 alert 框中用 each() 方法来一个个打印它们,代码如下:

<span style="font-size: 14px;">$(&#39;[name=NameOfSelectedTag] :selected&#39;).each(function(selected) {<br/>    alert($(selected).text());<br/>});<br/></span>
Copy after login

其中 text() 方法返回选项的文本。

9. 你是如何将一个 HTML 元素添加到 DOM 树中的?(答案如下)

你可以用 jQuery 方法 appendTo() 将一个 HTML 元素添加到 DOM 树中。这是 jQuery 提供的众多操控 DOM 的方法中的一个。你可以通过 appendTo() 方法在指定的 DOM 元素末尾添加一个现存的元素或者一个新的 HTML 元素。

10. 你能用 jQuery 代码选择所有在段落内部的超链接吗?(答案略)

这是另一个关于选择器的 jQuery 面试题。就像其他问题那样,只需一行 jQuery 代码就能搞定。你可以使用下面这个 jQuery 代码片段来选择所有嵌套在段落(

标签)内部的超链接(标签)……

11. $(this) 和 this 关键字在 jQuery 中有何不同?(答案如下)

这对于很多 jQuery 初学者来说是一个棘手的问题,其实是个简单的问题。$(this) 返回一个 jQuery 对象,你可以对它调用多个 jQuery 方法,比如用 text() 获取文本,用val() 获取值等等。而 this 代表当前元素,它是 JavaScript 关键词中的一个,表示上下文中的当前 DOM 元素。你不能对它调用 jQuery 方法,直到它被 $() 函数包裹,例如 $(this)。

12. 你如何使用jQuery来提取一个HTML 标记的属性 例如. 链接的href? (答案)

attr() 方法被用来提取任意一个HTML元素的一个属性的值. 你首先需要利用jQuery选择及选取到所有的链接或者一个特定的链接,然后你可以应用attr()方法来获得他们的href属性的值。下面的代码会找到页面中所有的链接并返回href值:

13. 你如何使用jQuery设置一个属性值? (答案)

前面这个问题之后额外的一个后续问题是,attr()方法和jQuery中的其它方法一样,能力不止一样. 如果你在调用attr()的同时带上一个值 例如. attr(name, value), 这里name是属性的名称,value是属性的新值。

14. What is the difference between detach() and remove() methods in jQuery? (Answer)

##Although detach( ) and remove() methods are both used to remove a DOM element. The main difference between the two is that detach() keeps track of past detached elements, so it can be undetached, while the remove() method will keep a reference to the object that was removed. You can also look at the appendTo() method for adding elements to the DOM.

15. How can you use jQuery to add an element to a Add and remove CSS classes from elements? (Answer)

By using the two jQuery methods addClass() and removeClass(). Dynamically changing the class attribute of elements can be simple. For example, use the class ".active" to mark their inactive and active states, etc.

16. Use CDN to load the jQuery library What are the main advantages? (Answer)

This is a slightly more advanced jQuery question. Well, in addition to the many benefits of error reporting, saving server bandwidth and faster download speeds, the most important thing is that if the browser has already downloaded the same jQuery version from the same CDN, then it will not download it again. Once. So today, many public websites use jQuery for user interaction and animation. If the browser already has a downloaded jQuery library, the website can have a very good display opportunity.

17. What is the difference between jQuery.get() and jQuery.ajax() methods?

ajax() method is more Powerful and more configurable, allowing you to specify how long to wait and how to handle errors. The get() method is a specialized method that just gets some data.

18. What is method chaining in jQuery? What are the benefits of using method chaining?

Method chaining is to call another method on the result returned by one method, which makes the code concise and clear. At the same time, because only one round of DOM search is performed, the performance is better.

19. What happens if you return false in a jQuery event handler?

This is typically used to prevent events from bubbling up.

20. Which method is more efficient: document.getElementbyId("myId") or $("#myId")?

The first one, because it directly calls the JavaScript engine.

The above is the detailed content of Common jQuery interview questions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template