如何将iframe中的值发送到div元素?

今天,我在iframe中做了一个文本编辑器。我知道来自iframe的值不会发送到数据库。我的第一个想法依赖于将值从 iframe 发送到 ,并且数据以各自的文本格式发送到 DataBase。divdiv


function 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>


慕少森
浏览 172回答 2
2回答

慕的地8271018

关于您正在执行的操作,要了解的一件重要事情是浏览器具有有关跨文档通信的规则。不允许 iframe&nbsp;中的页面访问或修改其父级的 DOM,反之亦然,除非两者具有相同的源。因此,以不同的方式放置它:从一个源加载的文档或脚本无法从另一个源获取或设置文档的属性。以下是MDN关于同源政策的文档。如果iframe和加载它的页面是同一来源的,你应该能够完全按照你想要做的事情去做。如果没有,则需要在同一来源提供这些文件才能这样做。

蝴蝶刀刀

嗯,我现在明白了!你可以用ajax或javascript获得元素,如下所示:示例的 JavaScript这里显示的 JavaScript 只是一个示例,用于演示如何访问 iframe 元素。// attach handlers once iframe is loadeddocument.getElementById('ifrm').onload = function() {&nbsp; &nbsp; // get reference to form to attach button onclick handlers&nbsp; &nbsp; var form = document.getElementById('demoForm');&nbsp; &nbsp; // set height of iframe and display value&nbsp; &nbsp; form.elements.button1.onclick = function() {&nbsp; &nbsp; &nbsp; &nbsp; var ifrm = document.getElementById('ifrm');&nbsp; &nbsp; &nbsp; &nbsp; var ht = ifrm.style.height = '160px';&nbsp; &nbsp; &nbsp; &nbsp; this.form.elements.display.value = 'The iframe\'s height is: ' + ht;&nbsp; &nbsp; }&nbsp; &nbsp; // increment and display counter variable contained in iframed document&nbsp; &nbsp; form.elements['button2'].onclick = function() {&nbsp; &nbsp; &nbsp; &nbsp; // get reference to iframe window&nbsp; &nbsp; &nbsp; &nbsp; var win = document.getElementById('ifrm').contentWindow;&nbsp; &nbsp; &nbsp; &nbsp; var counter = ++win.counter; //&nbsp; increment&nbsp; &nbsp; &nbsp; &nbsp; this.form.elements['display'].value = 'counter in iframe is: ' + counter;&nbsp; &nbsp; }&nbsp; &nbsp; // reference form element in iframed document&nbsp; &nbsp; form.elements.button3.onclick = function() {&nbsp; &nbsp; &nbsp; &nbsp; var re = /[^-a-zA-Z!,'?\s]/g; // to filter out unwanted characters&nbsp; &nbsp; &nbsp; &nbsp; var ifrm = document.getElementById('ifrm');&nbsp; &nbsp; &nbsp; &nbsp; // reference to document in iframe&nbsp; &nbsp; &nbsp; &nbsp; var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;&nbsp; &nbsp; &nbsp; &nbsp; // get reference to greeting text box in iframed document&nbsp; &nbsp; &nbsp; &nbsp; var fld = doc.forms['iframeDemoForm'].elements['greeting'];&nbsp; &nbsp; &nbsp; &nbsp; var val = fld.value.replace(re, '');&nbsp; &nbsp; &nbsp; &nbsp; // display value in text box&nbsp; &nbsp; &nbsp; &nbsp; this.form.elements.display.value = 'The greeting is: ' + val;&nbsp; &nbsp; }&nbsp; &nbsp; form.elements.button4.onclick = function() {&nbsp; &nbsp; &nbsp; &nbsp; // get reference to iframe window&nbsp; &nbsp; &nbsp; &nbsp; var win = document.getElementById('ifrm').contentWindow;&nbsp; &nbsp; &nbsp; &nbsp; win.clearGreeting(); // call function in iframed document&nbsp; &nbsp; }}或者你可以在jquery中做这样的事情:&nbsp; &nbsp; var content=$("iframe").contents().find('body').html();&nbsp; &nbsp;//alert elements in iframe or show elements as a response in and div&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; alert(content);
打开App,查看更多内容
随时随地看视频慕课网APP