简体   繁体   English

javascript将元素中的文本复制到textarea中

[英]javascript copy text from element into textarea

I'm using CKEditor Inline mod and I am trying to save this into database. 我正在使用CKEditor内联模块,并且试图将其保存到数据库中。 I have an idea about copy text from element to textarea. 我有一个关于将文本从元素复制到文本区域的想法。 I have element div#textToBeSaved and I want to copy all text from this element into textarea with name content immediately. 我有一个元素div#textToBeSaved ,我想立即将所有来自此元素的文本复制到具有名称content textarea中。

Header: 标头:

  <script type="text/javascript" src="/administration/aditional/ckeditor/ckeditor.js"></script>
  <script type="text/javascript">
    setTimeout(function() { copyText(); },500);

    function copyText() {    
        var data = CKEDITOR.instances.textToBeSaved.getData();
        document.getElementsByName("content")[0].value = data;
    }
  </script>

Text: 文本:

    <div id="textToBeSaved">
     <div id="content-full">
      <div class="container">
       {P_CONTENT}
      </div>
     </div>
    </div>
    <textarea name="content"></textarea>

{P_CONTENT} is parsed in PHP. {P_CONTENT}用PHP解析。 For example {P_CONTENT} returns <p contenteditable='true'>hello world</p> 例如{P_CONTENT}返回<p contenteditable='true'>hello world</p>

I just need save everything in the element #textToBeSaved into database with actual data. 我只需要将所有内容与实际数据一起保存在#textToBeSaved元素中即可

Thanks for any help. 谢谢你的帮助。

If your CKEditor has an id of textToBeSaved then this code will get the contents of the CKEditor for you and copy them into your content element: 如果您的CKEditor的id为textToBeSaved那么此代码将为您获取CKEditor的内容并将其复制到您的content元素中:

<script>
    setTimeout(function() { copyText(); },500);

    function copyText() {    
        var data = CKEDITOR.instances.textToBeSaved.getData();
        document.getElementsByName("content")[0].value = data;
    }
</script>

http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-getData http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-getData

By the way CKEDITOR.instances.textToBeSaved.getData(); 通过CKEDITOR.instances.textToBeSaved.getData(); is undefined. 未定义。 I think it's because element textToBeSaved isn't defined as editable. 我认为这是因为未将元素textToBeSaved定义为可编辑的。 But I can't mark it as editable 'cause I don't want to make everything editable in this element. 但是我不能将其标记为可编辑的,因为我不想使此元素中的所有内容都可编辑。 Here's a picture LINK 这是图片LINK

Any ideas please? 有什么想法吗?

You need to use the id of the element that you're editing (the one with the contentEditable attribute). 您需要使用要编辑的元素的ID(具有contentEditable属性的ID)。 If you refer to something else it's obvious that it can't work. 如果您引用其他内容,很明显它是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM