2012-03-03 43 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