使用JQuery检查iframe是否具有特定的id



我有一个函数可以调用iFrame内部的另一个函数,如下所示:

function globalFunc(value) {
//select and add ID to the iFrame
const preloading_iFrame = $("iframe[src*='http://127.0.0.1:8887/Components/preloading/index.html']");
preloading_iFrame.attr("id","preloadingID");
// call iframe's function
document.getElementById('preloadingID').contentWindow.iframeFunc(value);
}

由于我想多次执行globalFunc,我想知道我们是否可以在第一次添加id之后绕过将其添加到iframe。

类似于检查iframe是否将preloadingID作为id,然后不要再添加它!

有什么解决办法吗?

不需要添加ID。您已经有了对元素的引用,只需使用它。

function globalFunc(value) {
//select and add ID to the iFrame
const preloading_iFrame = $("iframe[src*='http://127.0.0.1:8887/Components/preloading/index.html']");
// call iframe's function
preloading_iFrame[0].contentWindow.iframeFunc(value);
}

对jQuery集合进行索引将返回相应的DOM元素。

最新更新