国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

動(dòng)態(tài)加載js、css等文件跨iframe實(shí)現(xiàn)

2019-11-20 21:03:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
1、動(dòng)態(tài)加載js,css文件(用原生js和jquery)

iframe結(jié)構(gòu):
frame0(父)
frame2(子)
frame3(子)

frame2中觸發(fā)事件,動(dòng)態(tài)的向frame3中 加載js、css文件和 dom元素?

*同級(jí)之間可以調(diào)用,可以 通過(guò) 子-父-子 的方式調(diào)用同級(jí)
parent.parentFram(“這個(gè)方法在調(diào)用其他子farme”);

1.jquery的append()
復(fù)制代碼 代碼如下:

速度快,同步(需要引入jquery)

var oBody = document.getElementById("frame3_id").contentWindow.$("body");

var str = "<div>...</div>"
var scriptTag = document.getElementById("frame3_id").contentWindow.document.getElementById("pop");
if(!scriptTag){
oBody.append(str);
}

var oScript= document.createElement("script");
oScript.id = "oScript1";
oScript.type = "text/javascript";
oScript.src="/test.js";
var oTag1 = document.getElementById("frame3_id").contentWindow.document.getElementById("oScript1");
if(!oTag1){
oBody.append(oScript);
}

document.getElementById("frame3_id").contentWindow.test(); // 調(diào)用frame3_id 中的test()方法

***********************************
上述例子:a.需要引入jquery;
***********************************
2.js 的appendChild()

速度慢,異步(需要判斷js是否加載完畢)

列子2:
復(fù)制代碼 代碼如下:

var str = "<div>...</div>"
var popDiv=document.createElement('div');
popDiv.style.xx = xxx;
popDiv.id = "pop";
popDiv.innerHTML = str;
var oBody = document.getElementById("frame3_id").contentWindow.document.getElementsByTagName("body")[0];
var oHead = document.getElementById("frame3_id").contentWindow.document.getElementsByTagName("head");

if(oHead && oHead.length){
oHead = oHead[0];
}else{
oHead = oBody;
}

var elemDivTag = document.getElementById("frame3_id").contentWindow.document.getElementById("pop");
if(!elemDivTag){
oBody.appendChild(popDiv)
}

var oScript= document.createElement("script"); (css類似)
oScript.id = "oScript1";
oScript.type = "text/javascript";
oScript.src="/test.js";
var scriptTag = document.getElementById("main").contentWindow.document.getElementById("oScript1");
if(!scriptTag){
oHead.appendChild(oScript);
}
oScript.onload = oScript.onreadystatechange = function(){
if ((!this.readyState) || this.readyState == "complete" || this.readyState == "loaded" ){
document.getElementById("main").contentWindow.test(); // test()方法 在 引入的js文件中
}else{
console.info("can not load the oScript2.js file");
}
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 益阳市| 灌南县| 桑植县| 河曲县| 台南县| 大庆市| 海宁市| 巴南区| 巴彦淖尔市| 梁河县| 通海县| 岢岚县| 沁水县| 新安县| 孟州市| 富川| 六枝特区| 涟水县| 思茅市| 长乐市| 永清县| 文昌市| 甘肃省| 集安市| 清远市| 灵台县| 宝坻区| 罗源县| 峡江县| 平顶山市| 渑池县| 岱山县| 白城市| 庆阳市| 万盛区| 黔东| 吉隆县| 揭西县| 襄樊市| 西华县| 江达县|