我有一个简单的 iFrame ...
<iframe src="url" width="980px" height="90px" id="inviteFrame" name="inviteFrame" scrolling="no"></iframe>
我知道我无法捕获框架内元素上的单击事件,但是在 iFrame 本身上进行简单的单击或鼠标按下怎么样?
$('#inviteFrame').click(function() {
console.log('test');
$(this).attr('height', '140px');
});
这对我不起作用!
最佳答案
jQuery 有一个方法,名为 .contents() ,当在 iframe
元素上使用时,返回 iframe 的文档。
// Get a reference to the iframe document
var iframeDoc = $('#inviteFrame').contents().get(0);
现在您可以将事件绑定(bind)到它,获取尺寸,获取各种元素的样式等:
// 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');
关于jquery - iframe 上的点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10572227/