2012-03-03 45 views
0

我正在做一个Android Web目录,因此我不得不使用iframe。然而,我想问,是否可以点击iframe中的按钮并使代码在主页面中工作?iframe中的目标按钮?

这是我的代码:

 function initFastButtons() { 
     new FastButton(document.getElementById("CloseButton"), runclose); 
     new FastButton(document.getElementById("OpenButton"), runopen); 
    }; 
    function runclose() { 
     $('.full-preview-viewer').hide(); 
    }; 
    function runopen() { 
     $('.full-preview-viewer').show(); 
    }; 

而且这是在iframe中的按钮看起来像:

<input id="OpenButton" type="image" src="products/special/product1.png" name="image" width="336" height="593"></input> 

谢谢大家:)

回答

1

可以移动的定义为“runclose()”和“runopen()”添加到您的父窗口(创建iframe的文档)。然后,在您的iframe中,您可以修改您的initFastButtons()设置,以将这些函数引用为“parent.runclose”和“parent.runopen”。请注意,通常这种事情是有限的,当页面不是从同一个域请求时。

考虑这两个页面, “inner.html” 和 “outer.html”:

outer.html:

<html> 
<head> 
<script type="text/javascript"> 
window.do_action = function() { 
    alert('the button was clicked!'); 
} 
</script> 
</head> 
<body> 
    <iframe src="inner.html"></iframe> 
</body> 
</html> 

inner.html:

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('button').click(parent.do_action); 
}); 
</script> 
</head> 
<body> 
    <button>Click Me!</button> 
</body> 
</html> 
+0

Hey Jason,我有点困惑。我在[iframe](http://www.nightlifebratislava.com/demos/mobile7/special.html)中创建了一个单独的jquery代码,它执行一个执行'parent.runopen();'的函数。但这不起作用(它也不适用于PC,但这很正常)。 [这里是主页](http://www.nightlifebratislava.com/demos/mobile7) – pufAmuf 2012-03-03 18:18:50

+0

它现在有效,谢谢! – pufAmuf 2012-03-03 18:31:45

0

您可以点击使用以下技术的iframe中的按钮。

var $currentIFrame = $('#IFrameId'); 
$currentIFrame.contents().find("#OpenButton").trigger("click"); 
+0

嗨Starx,我认为这段代码会消除我的FastButton功能,它可以在按下按钮时禁用300ms等待Android设备。 – pufAmuf 2012-03-03 18:21:19