簡體   English   中英

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

[英]Check if iframe has specific id using JQuery

我有一個函數可以像這樣在 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 元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM