2 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
關(guān)于您正在執(zhí)行的操作,要了解的一件重要事情是瀏覽器具有有關(guān)跨文檔通信的規(guī)則。
不允許 iframe 中的頁(yè)面訪問(wèn)或修改其父級(jí)的 DOM,反之亦然,除非兩者具有相同的源。因此,以不同的方式放置它:從一個(gè)源加載的文檔或腳本無(wú)法從另一個(gè)源獲取或設(shè)置文檔的屬性。
以下是MDN關(guān)于同源政策的文檔。
如果iframe和加載它的頁(yè)面是同一來(lái)源的,你應(yīng)該能夠完全按照你想要做的事情去做。如果沒有,則需要在同一來(lái)源提供這些文件才能這樣做。

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
嗯,我現(xiàn)在明白了!你可以用ajax或javascript獲得元素,如下所示:
示例的 JavaScript
這里顯示的 JavaScript 只是一個(gè)示例,用于演示如何訪問(wèn) iframe 元素。
// attach handlers once iframe is loaded
document.getElementById('ifrm').onload = function() {
// get reference to form to attach button onclick handlers
var form = document.getElementById('demoForm');
// set height of iframe and display value
form.elements.button1.onclick = function() {
var ifrm = document.getElementById('ifrm');
var ht = ifrm.style.height = '160px';
this.form.elements.display.value = 'The iframe\'s height is: ' + ht;
}
// increment and display counter variable contained in iframed document
form.elements['button2'].onclick = function() {
// get reference to iframe window
var win = document.getElementById('ifrm').contentWindow;
var counter = ++win.counter; // increment
this.form.elements['display'].value = 'counter in iframe is: ' + counter;
}
// reference form element in iframed document
form.elements.button3.onclick = function() {
var re = /[^-a-zA-Z!,'?\s]/g; // to filter out unwanted characters
var ifrm = document.getElementById('ifrm');
// reference to document in iframe
var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
// get reference to greeting text box in iframed document
var fld = doc.forms['iframeDemoForm'].elements['greeting'];
var val = fld.value.replace(re, '');
// display value in text box
this.form.elements.display.value = 'The greeting is: ' + val;
}
form.elements.button4.onclick = function() {
// get reference to iframe window
var win = document.getElementById('ifrm').contentWindow;
win.clearGreeting(); // call function in iframed document
}
}
或者你可以在jquery中做這樣的事情:
var content=$("iframe").contents().find('body').html();
//alert elements in iframe or show elements as a response in and div
alert(content);
- 2 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)