获取iframe中元素的Y位置 [英] Get Y position of an element in an iframe
本文介绍了获取iframe中元素的Y位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为已经有人问过这个问题,我有一个iframe页面,其中有近10个文本字段,每当用户点击任一文本字段时,我都想知道该页面的Y位置.有可能吗?
I think this has been asked already, I have an iframe page where we have nearly 10 textfield's, whenever the user taps on any one of the textfield, I would like to know the Y position in that page. Is that possible?
推荐答案
这未经测试,但应为您指明正确的方向.
This is untested, but should point you in the right direction.
jQuery
$(document).ready(function(){
var iframe = $('iframe');
var inputs = iframe.find('input');
$.each(inputs, function(){
$(this).click(function(){
console.log($(this).offset().top));
})
})
})
香草Javascript
根据评论进行了更新.也未经测试.
Updated as per comment. Also untested.
为您的iframe提供一个ID,以使其更易于定位,然后:
var iframe = document.getElementById('iframe');
var inputs = Array.prototype.slice.call(iframe.getElementsByTagName('input'));
inputs.forEach(function(input,i){
input.addEventListener('click', function(){
console.log(this.getBoundingClientRect().top;
})
})
这篇关于获取iframe中元素的Y位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文