亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將iframe中的值發送到div元素?

如何將iframe中的值發送到div元素?

PHP
慕少森 2022-08-19 10:55:07
今天,我在iframe中做了一個文本編輯器。我知道來自iframe的值不會發送到數據庫。我的第一個想法依賴于將值從 iframe 發送到 ,并且數據以各自的文本格式發送到 DataBase。divdivfunction wlaczTrybEdycji(){    edytorTextowy.document.designMode = "On";}function execCmd(command){    edytorTextowy.document.execCommand(command, false, null);}function przeniesDane(){    var ramka = document.getElementById("ramka");    var dodiv = document.getElementById("daneIframe");    dodiv == ramka;}.textarea2{    width: 700px;    float: left;    height: 240px;    border: 2px solid black;    clear: both;    padding: 5px;}<body onload="wlaczTrybEdycji();">  <form action="" method="post">    <div class="EdytorTextowy">        <div class="przyciskiTextEdytor">            <button onclick="execCmd('bold');">BOLD</button>        </div>             <iframe name="edytorTextowy" class="textarea2" id="ramka"></iframe>          <div id="daneIframe">                    </div>        <button type="submit" name="wyslijText">WYSLIJ</button><!-- here send from "daneIframe" to DB (PHP)-->    </div>  </form></body>
查看完整描述

2 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

關于您正在執行的操作,要了解的一件重要事情是瀏覽器具有有關跨文檔通信的規則。

不允許 iframe 中的頁面訪問或修改其父級的 DOM,反之亦然,除非兩者具有相同的。因此,以不同的方式放置它:從一個源加載的文檔或腳本無法從另一個獲取或設置文檔的屬性。

以下是MDN關于同源政策的文檔。

如果iframe和加載它的頁面是同一來源的,你應該能夠完全按照你想要做的事情去做。如果沒有,則需要在同一來源提供這些文件才能這樣做。


查看完整回答
反對 回復 2022-08-19
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

嗯,我現在明白了!你可以用ajax或javascript獲得元素,如下所示:


示例的 JavaScript


這里顯示的 JavaScript 只是一個示例,用于演示如何訪問 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);


查看完整回答
反對 回復 2022-08-19
  • 2 回答
  • 0 關注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號