Home > Article > Web Front-end > 20 common jQuery interview questions and answers (share)
This article will share with you 20 common jQuery interview questions and answers. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
JavaScript is the standard language for client-side scripting, and jQuery makes writing JavaScript even 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.
Related recommendations: "jq tutorial" (Video)
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.
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.
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.
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(); });
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.
This question and answer follows the previous one. The main difference between the JavaScript window.onload event and the jQuery ready function is that the former not only waits for the DOM to be created but also waits for all external resources including large images, audio, and video to be fully loaded. If loading images and media content takes a long time, users will experience a significant delay in the execution of code defined on the window.onload event.
另一方面,jQuery ready() 函数只需对 DOM 树的等待,而无需对图像或外部资源加载的等待,从而执行起来更快。使用 jQuery $(document).ready() 的另一个优势是你可以在网页里多次使用它,浏览器会按它们在 HTML 页面里出现的顺序执行它们,相反对于 onload 技术而言,只能在单一函数里使用。鉴于这个好处,用 jQuery ready() 函数比用 JavaScript window.onload 事件要更好些。
这是面试里比较棘手的 jQuery 问题之一。这是个基础的问题,但是别期望每个 jQuery 初学者都知道它。你能用下面的 jQuery 选择器获取所有具备 multiple=true 的 14270eeb917b9c0865f33d36f514ce27 标签的选中项:
$('[name=NameOfSelectedTag] :selected')
这段代码结合使用了属性选择器和 :selected 选择器,结果只返回被选中的选项。你可按需修改它,比如用 id 属性而不是 name 属性来获取 221f08282418e2996498697df914ce4e 标签。
each() 函数就像是 Java 里的一个 Iterator,它允许你遍历一个元素集合。你可以传一个函数给 each() 方法,被调用的 jQuery 对象会在其每个元素上执行传入的函数。有时这个问题会紧接着上面一个问题,举个例子,如何在 alert 框里显示所有选中项。我们可以用上面的选择器代码找出所有选中项,然后我们在 alert 框中用 each() 方法来一个个打印它们,代码如下:
$('[name=NameOfSelectedTag] :selected').each(function(selected) { alert($(selected).text()); });
其中 text() 方法返回选项的文本。
你可以用 jQuery 方法 appendTo() 将一个 HTML 元素添加到 DOM 树中。这是 jQuery 提供的众多操控 DOM 的方法中的一个。你可以通过 appendTo() 方法在指定的 DOM 元素末尾添加一个现存的元素或者一个新的 HTML 元素。
你可以使用下面这个 jQuery 代码片段来选择所有嵌套在段落(e388a4556c0f65e1904146cc1a846bee标签)内部的超链接(3499910bf9dac5ae3c52d5ede7383485标签)
$( 'p a' );
这对于很多 jQuery 初学者来说是一个棘手的问题,其实是个简单的问题。$(this) 返回一个 jQuery 对象,你可以对它调用多个 jQuery 方法,比如用 text() 获取文本,用val() 获取值等等。而 this 代表当前元素,它是 JavaScript 关键词中的一个,表示上下文中的当前 DOM 元素。你不能对它调用 jQuery 方法,直到它被 $() 函数包裹,例如 $(this)。
attr() 方法被用来提取任意一个HTML元素的一个属性的值. 你首先需要利用jQuery选择及选取到所有的链接或者一个特定的链接,然后你可以应用attr()方法来获得他们的href属性的值。下面的代码会找到页面中所有的链接并返回href值:
$('a').each(function(){ alert($(this).attr('href')); });
前面这个问题之后额外的一个后续问题是,attr()方法和jQuery中的其它方法一样,能力不止一样. 如果你在调用attr()的同时带上一个值 。
对象.attr("name","value");name是属性的名称,value是这个属性的新值
对象.prop("name","value");
设置多个属性值:对象.attr("name":"value","name":"value")属性:属性值,属性:属性值
对于html元素本身就带有的固定属性(本身就带有的属性),在处理时,使用prop方法 可以操作布尔类型的属性
对于html元素我们自己定义的dom属性,在处理时,使用attr方法 不可以操作布尔类型的属性
; The DOM attribute values are "ID, href, class, and action". Obviously, the first three are inherent attributes, and the latter Action attribute is our own definition of
# & LT; a & GT; the element itself has no attributes itself. of. 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)
15. How do you use jQuery to add and remove CSS classes from an element? (Answer)
.addClass("class name") to add elements .remove() Delete style classes
16. What is the main advantage of using a CDN to load the jQuery library? (Answer)
17. What is the difference between jQuery.get() and jQuery.ajax() methods?
18. What is the method chain in jQuery? What are the benefits of using method chaining?
For more programming-related knowledge, please visit:
Programming TeachingThe above is the detailed content of 20 common jQuery interview questions and answers (share). For more information, please follow other related articles on the PHP Chinese website!