本文介绍了iframe上的点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的iFrame ...
I have a simple iFrame …
<iframe src="url" width="980px" height="90px" id="inviteFrame" name="inviteFrame" scrolling="no"></iframe>
我知道我无法捕捉框架中的元素的点击事件,在iFrame本身上的mousedown?
I know I can't catch click events on elements inside the frame but how about a simple click or mousedown on the iFrame itself?
$('#inviteFrame').click(function() {
console.log('test');
$(this).attr('height', '140px');
});
这对我不起作用!
推荐答案
jQuery有一个名为的方法,在 iframe
元素返回iframe的文档。
jQuery has a method, called .contents(), that when used on an iframe
element returns the document of the iframe.
// Get a reference to the iframe document
var iframeDoc = $('#inviteFrame').contents().get(0);
现在您可以将事件绑定到它,获取维度,获取各种元素的样式等:
Now you can bind an event to it, get the dimensions, get styles of various elements, etc:
// Get width of iframe document
$(iframeDoc).width();
// Get height of iframe document
$(iframeDoc).height();
// Bind event to iframe document
$(iframeDoc).bind('click', function( event ) {
// do something
});
// Get the style of an element in the iframe
$('div', iframeDoc).css('backgroundColor');
这篇关于iframe上的点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!