Javascript 从 TinyMCE 文本区域获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6541782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:12:37  来源:igfitidea点击:

Getting the value from a TinyMCE textarea

javascripttinymce

提问by Sanfly

I have a news editor for my site with which I am using TinyMCE. What I would like to be able to do is have a button (outside of the TinyMCE editor itself) that I can click to scan the textarea for any images, then list those images as options to use for a thumbnail image for said news article.

我的网站有一个新闻编辑器,我正在使用 TinyMCE。我希望能够做的是有一个按钮(在 TinyMCE 编辑器本身之外),我可以单击它来扫描文本区域中的任何图像,然后将这些图像列为选项以用于所述新闻文章的缩略图。

For an idea of what I mean, please see this link here: https://docs.google.com/leaf?id=0B05m73kzudwPNzUwZjkyNmItYjZkMy00NTdlLTlkNDctOGRhYThjMzNjNTM5&hl=en_US

要了解我的意思,请在此处查看此链接:https: //docs.google.com/leaf?id=0B05m73kzudwPNzUwZjkyNmItYjZkMy00NTdlLTlkNDctOGRhYThjMzNjNTM5&hl=en_US

My problem is that document.getElementById('NewsArticle').value is not returning anything when there is text in the textarea

我的问题是 document.getElementById('NewsArticle').value 在 textarea 中有文本时没有返回任何内容

The other potential problem is that whats shown in the textarea is not actual code, but images etc too so I wasn't sure it would even work in the first place, but since when the form is submitted the data[News][article] value is back into text, I thought there might be a chance.

另一个潜在的问题是 textarea 中显示的内容不是实际代码,而是图像等,所以我不确定它甚至可以在第一时间工作,但是因为提交表单时数据[新闻][文章]价值回到文本中,我认为可能有机会。

If anyone knows how to get either the content or code for the tinyMCE textarea, or has a better solution, I'd be interested to hear

如果有人知道如何获取 tinyMCE textarea 的内容或代码,或者有更好的解决方案,我很想听听

回答by Matthew Manela

TinyMce has an apifor accessing content from the editor.

TinyMce 有一个用于从编辑器访问内容的 api

This code will grab the html from the active editor:

此代码将从活动编辑器中获取 html:

// Get the HTML contents of the currently active editor
tinyMCE.activeEditor.getContent();

// Get the raw contents of the currently active editor
tinyMCE.activeEditor.getContent({format : 'raw'});

// Get content of a specific editor:
tinyMCE.get('content id').getContent()

回答by XtraSimplicity

Try

尝试

window.parent.tinymce.get('contentID').getContent();

For some reason, the stock-standard tinymce.get()call didn't work for me, so I tried this and it works. :)

出于某种原因,股票标准tinymce.get()电话对我不起作用,所以我尝试了这个并且它有效。:)

回答by Milan

Use below syntax, which will remove unwanted character from your input textarea....

使用以下语法,这将从您的输入文本区域中删除不需要的字符....

(((tinyMCE.get('YourTextAreaId').getContent()).replace(/(&nbsp;)*/g, "")).replace(/(<p>)*/g, "")).replace(/<(\/)?p[^>]*>/g, "");

回答by Sujith S

var temp = tinymce.get('textAreaName').save();
console.log(temp);

OR

或者

var temp =tinymce.get('textAreaName').getContent();
console.log(temp);

回答by user3558547

Probably you have something like

可能你有类似的东西

<form>
  <textarea id="myArea">Hello, World!</textarea>
</form>

you should simply add as follows

你应该简单地添加如下

<form method="post">
  <textarea id="myArea" name="value">Hello, World!</textarea>
  <input type="submit">
</form>

and you can catch the data with PHP under myArea var.

并且您可以在 myArea var 下使用 PHP 捕获数据。