从父页面调用 iframe 中显示的子页面的 jquery 函数



我有一个页面,点击一个div会调用一个函数:

$("#fontOpenSans").on("click", function(event) {
    event.stopPropagation();
    $("#dlVersion *").css("font-family", $(this).css("font-family"));
    localStorage.font_family = $(this).css("font-family");
});

我现在在 iframe 中显示此页面,我想从此页面的外部调用相同的函数(从放置 iframe 的页面)。

如果我这样声明:

function callfontOpenSans() {
    event.stopPropagation();
    $("#dlVersion *").css("font-family", $(this).css("font-family"));
    localStorage.font_family = $(this).css("font-family");
}

那我可以这样称呼它:

$('#iframeID').contentWindow.callfontOpenSans();

我将如何从页面外部(即父页面)调用以第一种方式编写的函数?提前谢谢。

试试这个:

$('#iframeID')[0].contentWindow["callfontOpenSans"]();

希望这会有所帮助。

你好朋友们,我的问题得到了答案

$('#iframeId').contents().find('#fontOpenSans').click();

解决了我的问题 谢谢

当两个页面位于同一域中时将起作用

最新更新