js获取iframe对象,跨域操作?

//通过这样即可获取iframe中的元素
document.getElementById("myFrame").contentWindow.document.getElementById('ss').innerHTML;

// 等价于
window.frames[0] // is the same thing as 
document.getElementsByTagName("iframe")[ 0 ].contentWindow


var frames = window.frames; // or // var frames = window.parent.frames;
for (var i = 0; i < frames.length; i++) { 
  // do something with each subframe as frames[i]
  frames[i].document.body.style.background = "red";
}


window.onload = (function () { 
var iObj = document.getElementById('iId').contentWindow; 
alert(iObj.document.getElementsByTagName('h1')[0].firstChild.data); 
});
//此方法经过ie6,ie7,firefox2.0,firefox3.0测试都通过


// firefox下访问操作iframe里内容
var iObj = document.getElementById('iId').contentDocument; 
alert(iObj.getElementsByTagName('h1')[0].innerHTML='我想变成她一天的一部分'); 
alert(iObj.getElementsByTagName('p')[0].firstChild.data);


//通过designMode(设置文档为可编辑设计模式)和contentEditable(设置内容为可编辑),你可以重写iframe里的内容。代码:

var iObj = document.getElementById('iId').contentWindow; 
iObj.document.designMode = 'On'; 
iObj.document.contentEditable = true; 
iObj.document.open(); 
iObj.document.writeln('<html><head>'); 
iObj.document.writeln('<style>body {background:#000;font-size:9pt;margin: 2px; padding: 0px;}</style>'); 
iObj.document.writeln('</head><body></body></html>'); 
iObj.document.close();


// JS获取iframe元素及跨域访问操作
http://www.ribenyu.cn/space-1-do-blog-id-3443.html

你可能感兴趣的:(js,object,iframe)