[英]JQuery finding an element within Iframe (by its text) and adding .click function to it
我有一個網頁(A),我使用iframe將(A)嵌入到我的主頁(B)。 這個(A)包含一個關閉瀏覽器窗口的鏈接:
<a href="" onclick="window.opener = window;
window.close();
return false;">Close The Page</a>
由於我嵌入(A),(A)中的緊密能力不再具有功能性。 我需要做的是,當從(B)點擊此鏈接時,我想要隱藏我的iframe,換句話說,讓它看起來像關閉。 所以我必須在iframe中找到該鏈接並了解它是否被點擊(B)?
請幫忙。
謝謝
我想以下是可行的。 我們基本上對您的iframe內容執行“查找”。 找到我們想要的鏈接后,我們將一個事件綁定到它,它將關閉父文檔中的正確iframe。 請注意,您的iframe必須與您的父網頁位於同一個域中,否則您將無法訪問其元素。 另外,我在鏈接中添加了一個類,以便於選擇。 我建議你也這樣做。
$("#iframeID").contents().find("a.closeWindow").bind("click", function(){
$("#iframeID", top.document).hide();
});
如果您絕對需要將綁定基於鏈接的文本,則必須循環訪問鏈接以找到正確的鏈接:
$("a", $("#iframeID").contents()).each(function(){
if ($(this).text() == "Close The Page") {
$(this).bind("click", function() {
$("#iframeID", top.document).hide();
});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.