本文介绍了获取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位置
发布评论