使用Jquery在iframe中更改div [英] Changing div in iframe using Jquery
问题描述
我有一个Page A。在此页面中显示$ B $ c> iframe ,其中显示Page B。
I have a "Page A". In this page is an iframe
which displays "Page B".
<iframe id="iframeD" src="https://trello.com" width="600" height="600">
<p>Your browser does not support iframes.</p>
</iframe>
<div id="cTitle">Hello</div>
(有关工作示例,请参阅以下小提琴: http://jsfiddle.net/noscirre/yYkUN/ )
(See the following fiddle for a working example: http://jsfiddle.net/noscirre/yYkUN/)
Page B包含 div
,ID landingHeader
。如何在Page A上创建一个jQuery调用,用另一个 div
替换这个 div
(其内容在页面A),ID cTitle
?
"Page B" contains a div
with id landingHeader
. How can I create a jQuery call on "Page A" replace this div
with another div
(whose content is found on "Page A") with id cTitle
?
推荐答案
假设你'不违反同源政策,
1,为您的iframe提供 ID - < iframe id =frmsrc =javascript:undefined;>< / iframe>
2,获取窗口来自框架的
和 HTMLDocument
1, Give your iframe an id - <iframe id="frm" src="javascript:undefined;"></iframe>
2, Get the Window
and HTMLDocument
from the frame
var iWin = document.getElementById('frm').contentWindow,
iDoc = iWin.document;
3,Ceck在iframe中存在jQuery
3, Ceck for existance of jQuery in iframe
if( ! iWin.$ ){
var jq = iDoc.createElement('script');
jq.type = 'text/javascript';
jq.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js';
jq.onload = ready;
iDoc.head.appendChild(jq);
}else{
ready(); // the stuff you want to do
}
4,用jQuery做你想做的事
4, Do what you want with jQuery
function ready(){
var div = iWin['$']('#frm_div')[0]; // example from fiddle
div.textContent = 'bar';
}
查看此 小提琴 。
这篇关于使用Jquery在iframe中更改div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!