简体   繁体   English

如何删除粘贴上的p标签以外的其他html标签?

[英]How to remove other html tags except p tag on Paste?

I am having a little trouble with my inline text editor. 我的内联文本编辑器遇到了一些麻烦。 it is simple user has permission to copy paste on the textarea div. 很简单,用户有权在textarea div上复制粘贴。 no problem with that. 没问题。 but i dont want let them paste html with images, and div elements. 但我不想让他们将HTML贴上图片和div元素。

I have used 我用过

valid_elements: "p,br,b,i,strong,em",

it removes the style of p tags content. 它删除了p tags内容的样式。

to do this but this is not the solution according to my requirements. 为此,但这不是根据我的要求的解决方案。

and i also tried with paste_postprocess but it didn't do anything with latest version of tinymce. 并且我也尝试了paste_postprocess但是对于最新版本的tinymce却没有做任何事情。

and i have also tried many solutions which are already posted in this community. 而且我还尝试了许多已经发布在此社区中的解决方案。 but none of them work for me because i am using the latest version tinymce 4.0.26 . 但是它们都不适合我,因为我使用的是最新版本的tinymce 4.0.26

i know i can prevent copy paste by disabling right click. 我知道我可以通过禁用右键单击来防止复制粘贴。 but that will not be a good idea. 但这不是一个好主意。

Is there any way to filter only p tag with style from html content? 有什么方法可以从html内容中仅过滤带有style p tag

So if anybody has worked on copy paste with the latest versions of tinymce . 因此,如果有人使用过tinymce的最新版本on copy paste

Please help. 请帮忙。

You need to explicitly tell TinyMCE what attributes to keep when using the valid_elements option. 您需要明确告知TinyMCE使用valid_elements选项时要保留哪些属性。 For example, using your previous valid_elements list, you might do something like this: 例如,使用先前的valid_elements列表,您可以执行以下操作:

valid_elements: "p[style],br,b,i,strong,em"

This tells TinyMCE to only keep the tags listed and to keep any style attributes defined for p tags. 这告诉TinyMCE仅保留列出的标签,并保留为p标签定义的任何style属性。 Alternatively, you can also include all attributes for a specific element by doing this: 另外,您还可以通过执行以下操作来包括特定元素的所有属性:

valid_elements: "p[*],br,b,i,strong,em"

Again, this tells TinyMCE to keep all of the tags listed, but for the p tag, keep every attribute defined. 再次,这告诉TinyMCE保留列出的所有标签,但是对于p标签,请保留定义的每个属性。

For more information on the syntax of this valid_elements selector, check out this page . 有关此valid_elements选择器的语法的更多信息,请查看本页

Bad condition is: when I pasted the content in editor with img, div, p 糟糕的情况是:当我使用img,div,p将内容粘贴到编辑器中时
then after pasting content it shows all the element inside the box 然后粘贴内容后,它会显示框内的所有元素
(AFTER PASTING ON EDITOR) (在编辑器上粘贴之后)

I just followed to the whole process paste->savetext. 我只是遵循了粘贴->保存文本的整个过程。 Issue was tinymce does nothing when pasting the content but when I call the tinymce function 问题是tinymce在粘贴内容时什么都不做,但是当我调用tinymce函数时

tinymce.activeEditor.getContent();

It gives me pure content without img,div . 它给了我纯洁的内容而没有img,div So I thought why not to do that process on paste 所以我想为什么不对on paste进行该处理

So I just simply added this code in tinymce setup section: 因此,我只是在tinymce setup部分中添加了以下代码:

setup : function(ed) {
        ed.on('paste',function(e){
           setTimeout(function(){
               var textContent = tinymce.activeEditor.getContent();
                   tinymce.activeEditor.selection.setContent(textContent.trim());
           },200);  
        });
   }

and now everything working fine. 现在一切正常。

and Thanks @Nate Kibler for your help. 感谢@Nate Kibler的帮助。

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

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