JQuery在Iframe中查找元素(通过其文本),并添加.click函数 [英] JQuery finding an element within Iframe (by its text) and adding .click function to it

查看:183
本文介绍了JQuery在Iframe中查找元素(通过其文本),并添加.click函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页(A),我正在使用iframe嵌入(A)到我的主页(B)。这个(A)包含一个关闭浏览器窗口的链接:

 < a href =onclick =window.opener = window; 
window.close();
return false;>关闭页面< / a>


$ b

由于我嵌入(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()==关闭页面){
$(this).bind(click,function(){
$(#iframeID,top.document).hide();
});
}
});


I have a webpage (A) and I am embedding (A) to my mainpage (B) using iframe. This (A) contains a link which closes the browser window:

<a href="" onclick="window.opener = window;
    window.close();
    return false;">Close The Page</a>

Since I embedd (A), the close ability in (A) is no more functional. What I need to do is, when this link is clicked from (B), I want to hide my iframe, in other words make it look like closing. So I have to reach that link in the iframe and understand if it is clicked or not (B)?

Help please.

Thanks

解决方案

I'm thinking the following will work. We're basically performing a "find" on the contents of your iframe. Once we find the link we want, we bind an event to it that will close the proper iframe in the parent document. Note that your iframe must be on the same domain as your parent page, or you will not be able to access its elements. Additionally, I added a class to the link to make it easier to selection. I suggest you also do this.

$("#iframeID").contents().find("a.closeWindow").bind("click", function(){
  $("#iframeID", top.document).hide();
});

If you absolutely need to base the binding on the text of the link, you'll have to cycle through the links to find the proper one:

$("a", $("#iframeID").contents()).each(function(){
  if ($(this).text() == "Close The Page") {
    $(this).bind("click", function() { 
      $("#iframeID", top.document).hide(); 
    });
  }
});

这篇关于JQuery在Iframe中查找元素(通过其文本),并添加.click函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆