将iframe值转移​​到textarea

时间:2015-12-29 19:39:30

标签: javascript jquery html css iframe

我正在创建一个提交帖子的应用,但我最初设计时考虑了textarea,我已经放入iframe创建一个富文本字段,将显示样式设置为隐藏textarea并想知道如何修改它以使用iframe值。

HTML

<div id="textWrap">
      <div class="border">
        <h1>Start Writing</h1><br />
        <input id="title" placeholder="Title (Optional)">

        <div id="editBtns">
          <button onClick="iBold()">B</button>
          <button onClick="iUnderline()">U</button>
          <button onClick="iItalic()">I</button>
          <button onClick="iHorizontalRule()">HR</button>
          <button onClick="iLink()">Link</button>
          <button onClick="iUnLink()">Unlink</button>
          <button onClick="iImage()">Image</button>
        </div>

        <textarea id="entry" name="entry" rows="4" cols="50" type="text"  maxlength="500" placeholder="Add stuff..."></textarea>
        <iframe name="richTextField" id="richTextField"></iframe><br />
        <button id="add">Submit</button>
        <button id="removeAll" onclick="checkRemoval()">Delete All Entries</button>
        <ul id="list"></ul>
        <ul id="titleHead"></ul>
      </div><!--end of border div-->
    </div><!--end of textWrap-->

这是提交帖子的JS。

//target all necessary HTML elements
var ul = document.getElementById('list'),
    removeAll = document.getElementById('removeAll'),
    add = document.getElementById('add');
    //richText = document.getElementById('richTextField').value;

//make something happen when clicking on 'submit'
add.onclick = function(){
  addLi(ul)
};

//function for adding items
function addLi(targetUl){
  var inputText = document.getElementById('entry').value, //grab input text (the new entry)
      header = document.getElementById('title').value, //grab title text
      li = document.createElement('li'), //create new entry/li inside ul
      content = document.createElement('div'),
      title = document.createElement('div'),

      //textNode = document.createTextNode(inputText + ''), //create new text node and give it the 'entry' text

      removeButton = document.createElement('button'); //create button to remove entries

  content.setAttribute('class','content')
  title.setAttribute('class','title')
  content.innerHTML = inputText;
  title.innerHTML = header;

  if (inputText.split(' ').join(' ').length === 0) {
    //check for empty inputs
    alert ('No input');
    return false;
  }


  removeButton.className = 'removeMe'; //add class to button for CSS
  removeButton.innerHTML = 'Delete'; //add text to the remove button
  removeButton.setAttribute('onclick', 'removeMe(this);'); //creates onclick event that triggers when entry is clicked

  li.appendChild(title); //add title textnode to created li
  li.appendChild(content); //add content textnode to created li
  li.appendChild(removeButton); //add Remove button to created li

  targetUl.appendChild(li); //add constructed li to the ul
}

//function to remove entries
function removeMe(item){
  var deleteConfirm = confirm('Are you sure you want to delete this entry?');
  if (deleteConfirm){var parent = item.parentElement;
  parent.parentElement.removeChild(parent)}
};

function checkRemoval(){
  var entryConfirm = confirm('Are you sure you want to delete all entries?');
  if (entryConfirm){
    ul.innerHTML = '';
  }
};

演示我正在努力参考.. http://codepen.io/Zancrash/pen/VemMxz

1 个答案:

答案 0 :(得分:0)

您可以使用本地存储将iframe值传递给父DOM。

或(使用此方法将值从iframe传递到父容器)

var iFrameValue = $('#iframe').get(0).contentWindow.myLocalFunction();
var iFrameValue = $('#iframe').get(0).contentWindow.myLocalVariable;

来自IFrame html

<script type="text/javascript">

var myLocalVariable = "text";

function myLocalFunction () {
    return "text";
}

</script>