📜  jQuery | html() 方法

📅  最后修改于: 2022-05-13 01:56:02.267000             🧑  作者: Mango

jQuery | html() 方法

jQuery 中的html() 方法用于设置或返回被选元素的innerHTML 内容。

句法:

  • 它返回第一个匹配元素的内容。
    $(selector).html()
  • 它设置匹配元素的内容。
    $(selector).html(content)
  • 它使用函数设置内容。
    $(selector).html(function(index, currentcontent))

参数:此方法接受上面提到和下面描述的两个参数:

  • content:它是强制性参数,用于指定所选元素的新内容。
  • 函数(index, currentcontent):它是一个可选参数,它指定一个函数,该函数返回所选元素的新内容。
    • index:用于返回元素在集合中的索引位置。
    • currentcontent:用于返回被选元素的当前HTML内容。

下面的例子说明了 jQuery 中的 html() 方法:

示例 1:此示例将内容设置为元素。





  
 
    
        jQuery html() Method
    
        
    
 
    
  
            
    

           GeeksForGeeks     

              

        Fade-in effect
        jQuery | html() Method     

                             

输出:

示例 2:此示例返回元素的第一个匹配项。



  
 
    
        jQuery html() Method
    
        
    
 
    
  
            
    

           GeeksForGeeks     

              

        jQuery | html() Method     

                             

输出:

示例 3:本示例使用函数设置内容。



  
 
    
        jQuery html() Method
    
        
    
 
    
  
            
    

           GeeksForGeeks     

              

        jQuery | html() Method     

                               

输出: