📜  jQuery | first() 与示例

📅  最后修改于: 2021-11-25 03:18:48             🧑  作者: Mango

first()是 jQuery 中的一个内置函数,用于从指定元素中选择第一个元素。
句法:

$(selector).first()

这里的选择器是所有元素的主类。

参数:它不接受任何参数。

返回值:返回被选元素中的第一个元素。

jQuery 代码来显示这个函数的工作:

代码#1:


  

    
    

  

  
    

Welcome to GeeksforGeeks !!!

    
        

This is the first statement.

    
    
       
        

This is the second statement.

    
    
       
        

This is the third statement.

    
    
  

在上面的代码中,第一个“div”元素的背景颜色发生了变化。
输出:

在这里,您还可以通过选择所选元素的“id”“class”来进行选择。
代码#2:


  

    
    

  

  
    

Welcome to GeeksforGeeks !!!

    
        

This is the first statement.

    
    
       
        

This is second statement.

    
    
       
        

This is the third statement.

    
    
  

在上面的代码中,第一个“main”类的元素被突出显示。
输出: