为了账号安全,请及时绑定邮箱和手机立即绑定

如何将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 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号