【问题标题】:Click event of Object of iframe does not workiframe 对象的点击事件不起作用
【发布时间】:2016-10-07 05:38:09
【问题描述】:

我想使用在 wordpress 页面中加载的 iframe 的点击事件。 我的代码如下:

jQuery('#iframe_id').load(function(){
        var iframe = jQuery('#iframe_id').contents();
        iframe.find("#logout_btn").click(function(){
            alert("test");
        });
    });

如果 iframe 在使用 http 的服务器上,则它可以工作,但如果 iframe 在使用 https 的服务器上,则不起作用。

任何帮助/建议将不胜感激。

【问题讨论】:

  • 还有其他方法吗?我正在 iframe 中加载 codeigniter 页面,单击注销按钮我需要取消设置由 wordpress 设置的会话。我的 wordpress 站点和 codeigniter 页面位于不同的服务器上。并且 codeigniter 有 https 请求。
  • 您可以向目标服务器发送 http 请求,而不是使用 iframe。请注意,同源策略也适用于 Ajax 请求,因此除非您的目标服务器接受 CORS,否则您不能在此处使用 Ajax。
  • @Dips i.hv 添加了答案见stackoverflow.com/questions/37677310/…

标签: javascript php jquery wordpress iframe


【解决方案1】:

使用 .find("body").on('click' 。见FIDDLE

$('#iframe_id').load(function(){
    $(this).contents().find("body").on('click','#logout_btn',function(event) { 
      alert('test'); 
    });
});
$('iframe').attr("src","JavaScript:'<button id=logout_btn>Button</button>'");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2018-06-15
    • 2017-08-22
    • 2012-11-07
    相关资源
    最近更新 更多