What does implicit iteration mean? jQuery selector implicit iteration

After using the jQuery selector to obtain elements, if you directly operate on the elements regardless of the number of elements obtained, implicit iteration will occur during the operation. Implicit iteration means that when there are actually multiple elements to be operated, jQuery will automatically operate on all elements. The sample code is as follows.

<div>第1个div</div>
<div>第2个div</div>
<div>第3个div</div>
<div>第4个div</div>
<script>
   console.log($("div"));
   // 使用css()方法修改元素 css样式,将背景色设为pink
   $("div").css("ackground”, "pink");   //对所有的div进行相同操作
</script>

Before using jQuery, if you want to implement the above operations with native JavaScript, you need to get a collection of elements first, then traverse the collection, take out each element, and then perform the operation. And jQuery has the effect of implicit iteration, developers do not need to manually traverse, jQuery will automatically process according to the number of elements.

Learn a trick:

The hierarchical selector can complete the acquisition between multi-level elements, as shown in Table 1.

level selector

Below we demonstrate the use of hierarchical selectors through code.

<ul>
  <li>我是ul的li</li>
  <li>我是ul的1i</li>
</uI>
<script>
  console.log($("ul li"));     //获取u1中的li
 </script>

Guess you like

Origin blog.csdn.net/cz_00001/article/details/131352515