Home  >  Article  >  Web Front-end  >  Check out these common jQuery interview questions to help you better understand the basics!

Check out these common jQuery interview questions to help you better understand the basics!

青灯夜游
青灯夜游forward
2022-02-28 10:32:272482browse

This article will summarize and share with you some common interview questions based on jQuery (with answer analysis) to help you better understand the basic knowledge. I hope it will be helpful to everyone.

Check out these common jQuery interview questions to help you better understand the basics!

Related recommendations: 2022 Big Front-End Interview Questions Summary (Collection)

jQuery Interview Questions and Answer

JavaScript is the standard language for client-side scripting, and jQuery makes writing JavaScript easier. You can achieve a lot more by just writing a few lines of jQuery code. It is one of the longest-used JavaScript libraries, and there are very few new projects that do not use jQuery but use native JavaScript. What this means for you as a Java web developer is that you will find many jQuery interview questions in a Java web development interview.

Earlier, most of them were HTTP, HTML, CSS and JavaScript, but lately, in addition to the basics of JavaScript, people also want to know if you are familiar with jQuery. These 16 jQuery questions are geared toward web developers, and are great for brushing up on key concepts before a phone or video interview. If you're new to jQuery, it can also help you better understand the basics and inspire you to discover more.

1. What is $() in jQuery library? (The answer is 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 several times, and even though it's so basic, it's often used to differentiate whether a developer knows jQuery or not. [Recommended learning: jQuery video tutorial]

2. There are 5 e388a4556c0f65e1904146cc1a846bee 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:

$('#LoginTextBox')  // Returns element wrapped as jQuery object with id='LoginTextBox'
$('.active') // Returns all elements with CSS class active.

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

4. How to use jQuery to hide an image when a button is clicked?

This is an event processing problem. jQuery provides great support for events like button clicks. You can use the following code to hide an image located by ID or class. You need to know how to set events for the button and execute the hide() method. The code is as follows:

$('#ButtonToClick').click(function(){
    $('#ImageToHide').hide();
});

5. What is the function $(document).ready()? Why use it? (answer)

This question is important and often asked. The ready() function is used to execute code when the document enters the ready state. jQuery allows you to execute code when the DOM is fully loaded (i.e. the HTML is fully parsed and the DOM tree is constructed). The biggest advantage of using $(document).ready() is that it works in all browsers, and jQuery helps you solve the cross-browser problem. Users who need to know more can click on the answer link to view the detailed discussion.

6. What is the difference between JavaScript window.onload event and jQuery ready function? (Answer)

  这个问答是紧接着上一个的。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 的 14270eeb917b9c0865f33d36f514ce27 标签的选中项:

$('[name=NameOfSelectedTag] :selected')

  这段代码结合使用了属性选择器和 :selected 选择器,结果只返回被选中的选项。你可按需修改它,比如用 id 属性而不是 name 属性来获取 221f08282418e2996498697df914ce4e 标签。

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

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

$('[name=NameOfSelectedTag] :selected').each(function(selected) {
    alert($(selected).text());
});

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

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

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

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

  你可以使用下面这个 jQuery 代码片段来选择所有嵌套在段落(e388a4556c0f65e1904146cc1a846bee标签)内部的超链接(3499910bf9dac5ae3c52d5ede7383485标签)

 $( 'p a' );

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值:

$('a').each(function(){
   alert($(this).attr('href'));
});

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

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

  • 对象.attr("name","value"); name是属性的名称,value是这个属性的新值

  • 对象.prop("name","value");

Set multiple attribute values: Object.attr("name":"value","name":"value") attribute:Attribute value,AttributeAttribute value

  The difference between attr and prop in jquery

Attributes with), when processing, use the prop method to operate Boolean type attributes

                                                                                                                                                                                       .

## & lt; a href = "#" ID = "link1" class = "btn" action = "delete" & gt; delete & lt;/a & gt;

This example & lt; a & gt; The dom attribute values ​​of the element include "id, href, class and action". Obviously, the first three are inherent attributes, while the latter action attribute is defined by ourselves

                                   There are no attributes. This is a custom dom attribute. When processing these properties, it is recommended to use the attr method. When using the prop method to obtain and set the value of a custom property, undefined values ​​will be returned.

For elements like checkbox, radio and select, the checked attributes correspond to "checked" and "selected". These are also inherent attributes, so you need to use the prop method to operate to get the correct answer

##14. What is the difference between the detach() and remove() methods in jQuery? (Answer) Although the 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 undocked. The remove() method will keep a reference to the object that was removed in the past. You can also look at the appendTo() method used to add elements to the DOM.

15. You How to use jQuery to add and remove CSS classes from an element? (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 activated states, etc.

           

.addClass("class name")

Add element .remove() Remove style class

16. What is the main advantage of using CDN to load the jQuery library? (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 will have a very good chance of being displayed.

17. What is the difference between jQuery.get() and jQuery.ajax() methods? The 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.


(Learning video sharing:

Web front-end introductory tutorial

)

The above is the detailed content of Check out these common jQuery interview questions to help you better understand the basics!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete