1. تحميل ملفات JS و CSS ديناميكيًا (باستخدام JS و JQuery الأصلي)
هيكل iframe:
Frame0 (الأب)
Frame2 (Sub)
Frame3 (Sub)
حدث تشغيل في Frame2 ، وتحميل ملفات JS ، و CSS وعناصر DOM ديناميكيًا في Frame3؟
*يمكن استدعاؤها بين نفس المستوى ، ويمكن استدعاء نفس المستوى من خلال طريقة الوالدين الوالدين والطفل
Parent.ParentFram ("هذه الطريقة تدعو مزارع الأطفال الأخرى") ؛
1. jquery إلحاق ()
نسخة الكود كما يلي:
سرعة سريعة ، متزامنة (تحتاج إلى إدخال jQuery)
var obody = document.getElementById ("frame3_id"). contentWindow. $ ("body") ؛
var str = "<viv> ... </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") ؛
إذا (! OTAG1) {
obody.append (oscript) ؛
}
document.getElementByid ("Frame3_ID"). contentWindow.test () ؛
******************************************
مثال أعلاه: أ.
******************************************
2.JS AppendChild ()
سرعة بطيئة ، غير متزامنة (تحتاج إلى تحديد ما إذا كان قد تم تحميل JS)
الدرس 2:
نسخة الكود كما يلي:
var str = "<viv> ... </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] ؛
}آخر{
ohead = obody ؛
}
var elemdivtag = document.getElementById ("frame3_id"). contentWindow.document.getElementById ("pop") ؛
if (! elemdivtag) {
obody.appendchild (popdiv)
}
var oscript = document.createElement ("Script") ؛
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 == "كاملة" || this.readyState == "loaded") {
document.getElementByid ("Main"). ContentWindow.test () ؛
}آخر{
console.info ("لا يمكن تحميل ملف oscript2.js") ؛
}
}