📌  相关文章
📜  如何使用 jQuery 获取 div 的高度?

📅  最后修改于: 2021-08-30 09:42:11             🧑  作者: Mango

在本文中,我们将学习如何使用 jQuery 获取 div 的高度。在 jQuery 中,height 方法用于获取 HTML 中任何元素的高度。 height 方法设置并返回 HTML 元素的高度。

方法一: height()方法返回第一个匹配元素的高度,height(value)方法设置所有匹配元素的高度。

// Returns the height of the first matched element
$(selector).height()

// Set the height of the all matched elements
$(selector).height(value);

所以在height()方法的帮助下,我们会找到div的高度。

// Returns the height of the first matched div
$("div").height()

例子:



  

    
    
  
    
      
    
    

  

  
    
    
        This is the div.         

                        
          

输出:

点击按钮前:

点击按钮后:

方法 2: jQuery使用innerHeight()方法检查元素的内部高度,包括填充。

句法:

$("param").innerHeight()

例子:



  

    
  
    
  
    

  

    
                

        Click on the button and check          the innerHeight of an element         (includes padding).     

  

输出:
在单击“单击我”按钮之前:

单击“单击我”按钮后:

方法三:该方法使用outerHeight()方法来查找指定元素的外部高度。元素的外部高度包括内边距和边框。

句法:

$(selector).outerHeight(includeMargin)

示例:此示例显示元素的外部高度。



  

    
  
    
    
  
    
    

  

    
        GeeksforGeeks     
                          

输出:
点击按钮前:

点击按钮后: