if 语句后的 jquery 回调
大家好,我在某些情况下无法找到有关创建回调的帮助。
我有一段代码,它将链接页面加载到 iframe 中,然后在按下另一个链接时更改 scr。
$(".iframe").hide();
$(".lnk").click(function(){
$(".iframe").show('slow')
;})
;
$(".frmclose").click(function(){
$(".iframe").hide('slow')
;})
;
上面的内容在 (document).ready 中运行 (由于某种原因它在内部不起作用)
function changeIframeSrc(id, url) {
if (!document.getElementById) return;
var el = document.getElementById(id);
if (el && el.src) {el.src = url;return false;}return true;}
下面是在此之外的链接
<a href="http://www.google.com" onclick="return changeIframeSrc('ifrm', this.href)" class="lnk">google</a>
:在此之前,我将 iframe 所在的 div 取消隐藏。我在该 div 中还有一个隐藏 div + iframe 的按钮。
我遇到的问题是,一旦 iframe 打开,然后通过 div 链接关闭,如果通过单击不同的链接重新打开 iframe,iframe 会取消隐藏以显示旧页面,然后发生更改。但我想要的是框架加载新页面(同时隐藏),然后取消隐藏以显示它。我想过在 iframe src 更改后使用回调,但我看不到在哪里实现它。
发生的示例 (单击 GDPH 按钮查看 iframe 的链接)
任何想法或帮助表示赞赏。
问候
B 斯通纳
Howdy guys, im having trouble finding help on creating a callback in certain situations.
I have a piece of code which loads a links page in to an iframe and then changes the scr if another link is pressed.
$(".iframe").hide();
$(".lnk").click(function(){
$(".iframe").show('slow')
;})
;
$(".frmclose").click(function(){
$(".iframe").hide('slow')
;})
;
The above runs within (document).ready
below is outside of this (for some reason it does not work on the inside)
function changeIframeSrc(id, url) {
if (!document.getElementById) return;
var el = document.getElementById(id);
if (el && el.src) {el.src = url;return false;}return true;}
the link :
<a href="http://www.google.com" onclick="return changeIframeSrc('ifrm', this.href)" class="lnk">google</a>
prior to this i have the div in which the iframe is in become unhidden. I also have a button within that div which hides the div + iframe.
What im having problems with is once the iframe has been opened and then closed via the div link if it is re-opened by clicking a different link the iframe unhides to display the old page then changes. But what i want is for the frame to load the new page(while hidden) then unhide to display it. I thought of using a callback after the iframe src change but i cant see where i would implement it.
Example of it happening
(click the GDPH button to see the links for the iframe)
Any thoughts or help appreciated.
Regards
B Stoner
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要做的就是在关闭时清除
的
src
。这将清除页面,以便下次显示 iFrame 时它会再次开始为空白。我制作了此功能的小演示,它使用了来自以您的页面为例。
开始隐藏(通过 CSS),每个链接将显示
,然后加载远程站点。我添加了一个关闭 iframe 链接来模拟您网站上
下的关闭链接。
希望这有帮助!
编辑:更新了演示链接以包含回调部分。当我读到这个问题时不知何故错过了这一点!
编辑2:您的
changeIframeSrc
函数不起作用是因为它是在jQuery匿名函数内定义的并且是一个闭包。请参阅从 javascript 调用 Jquery 函数I think that all you need to do is clear the
src
of the<iframe>
when it is closed. That will clear the page so that next time you show the iFrame it will start out blank again.I made a small demo of this functionality that uses the 3 links from your page as an example. The
<iframe>
starts hidden (by CSS) and each link will show the<iframe>
and then load the remote site. I added a close iframe link to simulate the close link you have under the<iframe>
on your site.Hope this helps!
Edit: Updated the demo link to include the callback part. Somehow missed that when I read the question!
Edit 2: Your
changeIframeSrc
function was not working was because it was defined inside the jQuery anonymous function and is a closure. See calling Jquery function from javascript我会捕获 iframe 的
.load()
事件,该事件将在文档加载到 iframe 后触发。I would catch the
.load()
event for the iframe, this will fire after the document has been loaded in to the iframe.