简体   繁体   English

如何在JQuery中检索div的高度?

[英]How can I retrieve the height of a div in JQuery?

I am absolutly new in JavaScript and JQuery. 我对JavaScript和JQuery绝对陌生。

I have a JQuery script that is performed each time the user click on a node of a JSTree : 我有一个JQuery脚本,每次用户单击JSTree的节点时都会执行

$("#treeId2").bind("select_node.jstree", function (event, data) {

    // DO SOMETHING

})

In this script I have to retrieve the height of the div having id=treeId2 在此脚本中,我必须检索具有id = treeId2的div的高度

How can I do this thing in JQuery? 如何在JQuery中执行此操作?

Tnx 特纳克斯

var heightInPixels = $('#treeld2').height();

Like this! 像这样!

$("#treeId2").height();

Or, in line with your question: 或者,根据您的问题:

$("#treeId2").bind("select_node.jstree", function (event, data) {

    alert($("#treeId2").height());

});

You can do it in some ways, for example: 您可以通过某些方式做到这一点,例如:

$('#element').height()           
$('#element').innerHeight()              
$('#element').outerHeight()              
$('#element').outerHeight(true)

Above functions count height element with various combination including - padding - border - margin - extact height of element 以上功能计算高度元素的各种组合,包括-填充-边框-边距-元素的实际高度

在此处输入图片说明 See diferences of each usage here 在这里查看每种用法的区别

You could do something like 你可以做类似的事情

$('#treeId2').on('click', function(){
 var height = $(this).height();
 //other code`
})

Reference (jquery docs are great): http://api.jquery.com/height/ 参考(jQuery文档很棒): http : //api.jquery.com/height/

You can use .height() to find an element height, but you should not be using .bind() , its deprecated post jQuery 1.7. 您可以使用.height()来查找元素的高度,但是您不应该使用.bind() ,它已弃用jQuery 1.7。 You can use .click() : 您可以使用.click()

$("#treeId2").click(function () {
   var height = $(this).height();
});

or you can use .on() : 或者您可以使用.on()

$("#treeId2").on("click", function () {
   var height = $(this).height();
});

jquery has the .height() method, so using something like: jQuery具有.height()方法,因此使用类似以下内容的方法:

$("#treeId2").height()

should return what you are looking for. 应该返回您正在寻找的东西。

It is worth noting that this does not take padding, margins, or borders into account. 值得注意的是,这并未考虑填充,边距或边界。

Source 资源

$("#treeId2").on("click", function () {
  var height = $(this).height();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM