Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

javascript - How to know the position of element with reference to another element

I am having static input element and I am adding dynamic input elements below the static element.

Please let me know how can I know the position of static element with reference to the dynamic input element which got changed. I want to get position of static input with reference to the change in dynamic input element.

<div>
<input type="text" class="form-control" id="tbxSave" style="width:150px;">
</div>
<div>
<div>
<tbody id=gridBody>
<tr>
<th>
<input type="text" class="form-control clsTbxValue" style="width:200px; height:20px;">
</th>
</tr>
<tr>
<th>
<input type="text" class="form-control clsTbxValue" style="width:200px; height:20px;">
</th>
</tr>
<tr>
<th>
<input type="text" class="form-control clsTbxValue" style="width:200px; height:20px;">
</th>
</tr>
</tbody>
</div>

<script>
 $(document).ready(function () {
    $("#gridBody").on("change",".clsTbxValue", function () {
    var txtBxPos=$("#tbxSave").position();
    });
});
</script>

but with the above code snippet, I am always getting the same position.

For example, I am trying to change the input in 3rd row, I need to get the distance like position.top() from current textbox to tbxSave input textbox. But with the current logic, I am always getting the same position.top() from any element which got changed.

Please let me know how can we achieve.

question from:https://stackoverflow.com/questions/66055154/how-to-know-the-position-of-element-with-reference-to-another-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use .offset.top and .offset.left and subtract them with the element which is currently changed offsets values to get required results.

Demo Code :

$(document).ready(function() {
  $("#gridBody").on("change", ".clsTbxValue", function() {
    //get top distance 
    var txtBxPos_From_top = $(this).offset().top - $("#tbxSave").offset().top;
    //get top distance 
    var txtBxPos_from_left = $(this).offset().left - $("#tbxSave").offset().left;
    console.log(" Left --> " + txtBxPos_from_left)
    console.log(" Top --> " + txtBxPos_From_top)

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <input type="text" class="form-control" id="tbxSave" style="width:150px;">
</div>
<div>
  <div>
    <table>
      <tbody id=gridBody>
        <tr>
          <th>
            <input type="text" class="form-control clsTbxValue" style="width:200px; height:20px;">
          </th>
        </tr>
        <tr>
          <th>
            <input type="text" class="form-control clsTbxValue" style="width:200px; height:20px;">
          </th>
        </tr>
        <tr>
          <th>
            <input type="text" class="form-control clsTbxValue" style="width:200px; height:20px;">
          </th>
        </tr>
      </tbody>
    </table>
  </div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...