需要注意點(diǎn):
所有的操作必須確保在$('iframename').load(function(){})內(nèi)進(jìn)行;
$('#iframe').load(function({
? ? ?所要進(jìn)行的操作
}))確保iframe裝載完畢之后進(jìn)行后續(xù)操作;不然你可能得到
var x = document.getElementById('iframe').contentWindow.document.getElementById('container');
console.log(x)?
打印臺(tái)結(jié)果為:// null
var x = document.getElementById('iframe').contentWindow.document
console.log(x)
打印臺(tái)結(jié)果為:
方法一:直接通過(guò)js或者jQ獲取iframe DOM節(jié)點(diǎn)進(jìn)行控制
如果要使用jQeury獲取frame內(nèi)元素的話必須確保iframe所引入的頁(yè)面內(nèi)引入了jQuery框架浪蹂;
$('#iframe').load(function () {
? ? var x = document.getElementById('iframe').contentWindow.document.getElementById('container');
? ? x.style.width = "100%";
? ? x.style.height = "100%";
? ? console.log(x)//以iframe中找元素a為例
});
方法二:創(chuàng)建link標(biāo)簽萎庭,添加到iframe中
$('#iframe').load(function () {
? ? var cssLink = document.createElement("link");
? ? cssLink.href = "style.css";
? ? cssLink.rel = "stylesheet";
? ? cssLink.type = "text/css";
? ? frames['iframe'].document.body.appendChild(cssLink);
});