jquery - iframe 上的点击事件?

标签 jquery iframe click

我有一个简单的 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/

相关文章:

html - 使用两个嵌套的 iframe 时防止打开新选项卡

html - 使透明的 <div> 对鼠标事件不透明

jquery - Mysql表列不为空,但我需要在codeigniter中为空列

javascript - 防止$('img').click触发多次

javascript - 有没有办法在网站中嵌入和设计维基百科文章的样式?

javascript - 如何使用 Html iframe 在 Angular 内运行 ionic 应用程序

javascript - 我是否遭受了过多的递归? JQuery - 在某些情况下必须点击按钮两次

C# 检查按钮是否被点击

javascript - jQuery 查看键是否存在于整个 JSON 中

Jquery hasClass + If 语句