简体   繁体   English

获取div滚动中输入的高度-javascript

[英]Get height of the input inside div scroll - javascript

I have inputs inside div overflow scroll. 我在div溢出滚动内有输入。 JSFIDDLE JSFIDDLE

Here when i click tab i should be able to get the height of that input. 在这里,当我单击tab我应该能够获得该输入的高度。

First input : height 10px; 首先输入:高度10像素; - clicked tab -点击的标签

Second input : height 20px; 第二输入:高度20像素;

I have the properties of activeElement 我有activeElement的属性

nextInput   {...}   [Object, HTMLInputElement]
[Methods]   {...}   er
clientTop   2   Number
clientWidth 562 Number
COMMENT_NODE    8   Number
complete    false   Boolean         
ng339   742 Number
nodeName    "INPUT" String
scrollHeight    46  Number
scrollLeft  0   Number
scrollTop   0   Number
scrollWidth 562 Number
selectionEnd    0   Number
selectionStart  0   Number
size    20  Number      
willValidate    false   Boolean

How to achieve this? 如何实现呢?

To calculate the distance of the element from the top of the div (after scrolling), you need to use the scrollTop property of the div and the offsetTop property of the input element. 要计算元素到div顶部的距离(滚动之后),您需要使用div的scrollTop属性和输入元素的offsetTop属性。 Assuming you have a reference to these elements, you make the calculation like this: 假设您对这些元素有引用,则进行如下计算:

var currentPosition = inputElement.offsetTop - divElement.scrollTop;

In order to do calculate the distance that each element is from the top of its parent, you're looking for .offsetTop . 为了计算每个元素到其父元素顶部的距离,您需要寻找.offsetTop

In the following example, I do this by looping over getElementsByTagName() : 在以下示例中,我通过遍历getElementsByTagName()实现此目的:

 var elements = document.getElementsByTagName('input'); for (var i = 0; i < elements.length; i++) { elements[i].onclick = function() { console.log(this.offsetTop); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="max-height:80px;overflow:auto;width:210px;"> <input type="text" style="height:10px"> <input type="text" style="height:20px"> <input type="text" style="height:30px"> <input type="text" style="height:20px"> </div> 

Note that .offsetTop only works on the immediate parent. 请注意, .offsetTop仅适用于直接父级。 If you'd like to calculate the distance from an element higher up in the DOM, you'll need to loop through all available parents and sum up their offsets. 如果要计算到DOM中较高元素的距离,则需要遍历所有可用的父对象并总结其偏移量。

Hope this helps! 希望这可以帮助! :) :)

i got solution @Matarishvan 我有解决方案@Matarishvan

here is my code you just need to use focus event and fetch height of that element 这是我的代码,您只需要使用focus事件并获取该元素的高度

<div >
   <form>
      <input type="text" placeholder="first input"></input>
      <input id="secondtext" type="text" placeholder="second text"></input>
   </form>
</div>

<script>

$(document).ready(function () {
    $("input").hover(function(){
      var val = $(this).height();
      console.log(val);
    });
})
</script>

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

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