【问题标题】:adding click listener to iframe which is dynamically created in another iframe将点击侦听器添加到在另一个 iframe 中动态创建的 iframe
【发布时间】:2014-01-12 00:21:12
【问题描述】:

我使用下面的代码将点击事件添加到 iframe

$("#myframe").load(function() {
    $(this.contentWindow.document).on('click', function() {
      alert("works fine");
    });
  })

它工作正常,但我需要一个内部 iframe 的点击侦听器,它是另一个 iframe(父 #myframe)的一部分。

【问题讨论】:

标签: javascript jquery


【解决方案1】:
$("#myframe").load(function() {
    $(this.contentWindow.document).on('click', function() {
        alert('for parent')
    });

    $(this.contentWindow.document).find('iframe').contents().on('click', function() {
        alert('for inner iframe')
    });
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2021-09-06
    • 2013-02-11
    相关资源
    最近更新 更多