我想從另一個框架調用一個函數。但是,當我使用「document.getElementById(frame_id))」它找不到。在另一層的其他iframe中調用一個函數
我檢查了文檔的innerHTML輸出是什麼,並且我需要定位的iframe是另一個iframe中的1層。
在主iframe中我使用: 的document.getElementById( '玩家')),但它retuns:空
我需要去的 「玩家」 的iframe中的 「菜單」 IFRAME ..
這裏是 「主」 I幀頭部代碼:
function callPlayer(frame_id, func, args){
if(!frame_id) return;
if(frame_id.id) frame_id = frame_id.id;
else if(typeof jQuery != "undefined" && frame_id instanceof jQuery && frame_id.length) frame_id = frame_id.get(0).id;
if(!document.getElementById(frame_id)) return;
args = args || [];
/*Searches the document for the IFRAME with id=frame_id*/
var all_iframes = document.getElementsByTagName("iframe");
for(var i=0, len=all_iframes.length; i<len; i++){
if(all_iframes[i].id == frame_id || all_iframes[i].parentNode.id == frame_id){
/*The index of the IFRAME element equals the index of the iframe in
the frames object (<frame> . */
window.frames[i].postMessage(JSON.stringify({
"event": "command",
"func": func,
"args": args,
"id": frame_id
}), "*");
}
}
}
function switchState()
{
callPlayer('player', 'stopVideo');
}
希望有人理解我的描述..
等待,爲什麼會出現在iframe的HTML標記? – 2012-03-16 12:22:05
該網站基於KusabaX 0.9.3框架。這是該網站的建立方式,我將無法改變.. – 2012-03-16 12:24:11
@JeffreySweeney,因爲iframe包含有效的HTML文檔,所以iframe的第一個孩子應該始終是HTML。 – tkone 2012-03-16 12:36:19