Javascript 使用JS调用用户定义的jQuery函数
我使用jQuery窗口库 它以这种方式触发事件$class of element.modaal{arg1,arg2,…} --我在这里更新了我的问题,使其更一般,并使用了iframe/Html而不是外部svg- 为了触发一个元素,例如在iframe中加载的外部Html中,我对iframe应用了以下代码:Javascript 使用JS调用用户定义的jQuery函数,javascript,jquery,Javascript,Jquery,我使用jQuery窗口库 它以这种方式触发事件$class of element.modaal{arg1,arg2,…} --我在这里更新了我的问题,使其更一般,并使用了iframe/Html而不是外部svg- 为了触发一个元素,例如在iframe中加载的外部Html中,我对iframe应用了以下代码: <iframe src="External.html" id="mainContent" onload="access()"></iframe> 实际上,它只会在每一秒
<iframe src="External.html" id="mainContent" onload="access()"></iframe>
实际上,它只会在每一秒的点击中起作用。知道我没有好好考虑什么吗?< /P>
最好的它不起作用的原因是iframe没有完全加载,而jQuery试图附加函数。由于$document.readyfunction{}不起作用,解决方法是使用
$( window ).on( "load",function() {
$("#mainContent").contents().find("IDofDIVelement").modaal({});
});
这可以正确地将功能性附加到iframe中的元素
实际上,在覆盖层再次打开和关闭后,modaal将使envent处理程序消失。
所以可能有人也想为modaal触发一个iframe元素,这里有一个解决这个问题的设置。
可以通过@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();
}
});
}
}
您无需等待windows加载,但只需等待iframe:
$(function() {
$("#mainContent").bind("load",function(){
var myIframeElement = $(this).contents().find(".modaal");
myIframeElement.modaal({
content_source: '#iframe-content',
type: 'inline',
});
});
});
JQuery是一种javascript。如果您能够在纯js中调用加载的svg中的元素,那么也可以使用JQuery进行调用。但由于Modaal是一个JQuery插件,您必须拥有JQuery对象。@SvenLiivak抱歉,我没有得到您的答案。即使我在iframe中使用Html,我也不能将其传递给jQuery事件。我可以在iframe文档上调用Javascript函数,如document.getElementByIdiframeID.contentDocument.getElementByIdelementID,但jQuery不会附加到此元素。是否有方法触发外部元素,例如使用jQuery的iframe中的元素?A应该是jquery元素,不是吗?$iframeID.contents.find elementId。modaal@SvenLiivak我想我已经试过了。我得到了rigth元素,但它只会在每一秒点击时起作用,不知道,为什么?控制台中没有错误消息$mainContent.contents.findidofDiveElement.modaal{};你不需要设置自己的倾听者,moodal会为你设置。演示代码中唯一需要的行是:$mainContent.contents.findidofDiveElement.modaal{};
$(function() {
$("#mainContent").bind("load",function(){
var myIframeElement = $(this).contents().find(".modaal");
myIframeElement.modaal({
content_source: '#iframe-content',
type: 'inline',
});
});
});