📜  jQuery | parent() 和 parents() 与示例

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

parent()是 jQuery 中的一个内置方法,用于查找与所选元素相关的父元素。 jQuery 中的这个 parent() 方法遍历所选元素的单个级别并返回该元素。
句法:

$(selector).parent()

这里的选择器是需要查找其父元素的选定元素。
参数:它不接受任何参数。
返回值:返回被选元素的父元素。
jQuery 代码来显示这个函数的工作:


  

    
    
          
    
    

  

  
  
    
div (Great-Grandparent)       
    This is the grand-parent of the selected span element.!       
  • This is the parent of the selected span element.!       This is the span element !!!       
  •       
    
   
  

在上面的代码中,只有所选元素的父元素是深绿色。
输出:

parent()是 jQuery 中的一个内置方法,用于查找与所选元素相关的所有父元素。 jQuery 中的这个 parents() 方法遍历所选元素的所有级别并返回所有元素。
句法:

$(selector).parents()

这里的选择器是所有父元素都需要找到的选定元素。
参数:它不接受任何参数。
返回值:返回被选元素的所有父元素。
jQuery 代码来显示这个函数的工作:


  

    




  

  
This is the great grand parent of the selected span element.!    
    This is the grand parent of the selected span element.!    
  • This is the parent of the selected element.!    This is the selected span element.!    
  •    
   
  

在上面的代码中,所选元素的所有父元素都显示为深绿色。
输出:

jQuery 是一个开源 JavaScript 库,它简化了 HTML/CSS 文档之间的交互,它以其“少写,多做”的理念而广为人知。
您可以按照此 jQuery 教程和 jQuery 示例从头开始学习 jQuery。