简体   繁体   English

用JS调用用户定义的jQuery函数

[英]Call user defined jQuery function with JS

I use a jQuery window libray https://github.com/humaan/Modaal 我使用jQuery窗口libray https://github.com/humaan/Modaal

which triggers events this way $("class of element").modaal({arg1, arg2,...}); 这样触发事件$("class of element").modaal({arg1, arg2,...});

--- I updated my question here to make it more general and used an iframe / Html instead of an external svg --- ---我在这里更新了我的问题,使其更通用,并使用了iframe / HTML而不是外部svg-

To trigger an element eg in an external Html which is loaded within an iframe, I applied the following code to the iframe: 为了触发一个元素,例如在iframe中加载的外部HTML中,我将以下代码应用于iframe:

<iframe src="External.html" id="mainContent" onload="access()"></iframe>

which calls this function: 调用此函数:

function access() {
var html = document.getElementById("mainContent").contentDocument.getElementById("IDofDIVelement");
html.addEventListener('click', function() {clicker();});
}
function clicker()
{
// console.log('hooray!');
$("#mainContent").contents().find("IDofDIVelement").modaal({});
//return false;
}

Actually it will only work on every second click. 实际上,它只会在第二次单击时起作用。 Any idea what I did not consider properly? 知道我没有适当考虑的事情吗?

Best 最好

You do not need to wait windows loading but iframe only: 您无需等待Windows加载,而只需要iframe:

$(function() {
    $("#mainContent").bind("load",function(){
        var myIframeElement = $(this).contents().find(".modaal");

        myIframeElement.modaal({
            content_source: '#iframe-content',
            type: 'inline',
        });
    });
});

The reason why it did not work was that the iframe was not completely loaded, while jQuery tried to attach the function. 之所以不起作用,是因为jQuery尝试附加该功能时,iframe并未完全加载。 As $(document).ready(function(){} did not work, the workaround was to initialize it with 由于$(document).ready(function(){}不起作用,解决方法是使用

$( window ).on( "load",function() {
$("#mainContent").contents().find("IDofDIVelement").modaal({});
});

This worked properly to attach the functionallity to an element within the iframe. 这可以正常工作,以将功能附加到iframe中的元素。

Actually modaal will vanish the envent handler after the overlay was opened and closed again. 实际上,在覆盖层再次打开和关闭后,模态将消失。 So maybe someone wants to trigger an iframe element for modaal, too, here is a setup which would solve this issue. 因此,也许有人也想触发iframe元素以获得模态,这是可以解决此问题的设置。 (It can be optimised by @SvenLiivaks answer): (可以通过@SvenLiivaks回答对其进行优化):

 $(window).on("load", function() {
     reload();
    });

function reload() {
    var length = $("#iframeID").contents().find("#IDofDIVelement").length;
    // The following check will return 1, as the iframe exists.
    if (length == 0) {
        setTimeout(function() { reload() }, 500);
    } else {
        $("#iframeID").contents().find("#IDofDIVelement").modaal({
            content_source: '#modalwrapper',
            overlay_close: true,
            after_close: function reattach() {
                reload();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM