如何使用 Jquery 编码以从文本编辑器中提取或转义某些特定的 html 标签
How to code with Jquery to extract or escape some specific html tags from a text editor
我正在尝试从来自文本编辑器 #Textarea 的字符串中删除一些特定的 html 标签。
我有一个文本区域,其中包含按钮单击事件。我得到了文本编辑器的所有内容,并想将这些内容分配给另一个 html 标签。但在分配之前,我想删除一些 HTML 元素,或者换句话说,我只想允许 HTML 标签(p,lists,table)。
我正在使用 tinyMCE 文本编辑器,我在按钮点击事件上的功能是
function AssignContents() {
// takes the specific tag ID
var ref=$('.data_button').attr('data_id');
var textEditorContent = tinyMCE.activeEditor.getContent();
$('#'+ref).html(textEditorContent);
}
但是当我在数据库中保存数据时 table 看起来很奇怪。
例如。如果我在文本编辑器 "hello wold" 中编写并使用按钮单击事件,则下面给出的函数将 tinyMCE 的内容分配给 textarea,并在表单提交后将其保存到数据库 table。数据库 table 中的值如下所示。
<p>Hello World!</p>
但我只想 <p>hello world </p>
存储在数据库中而不是其他东西。
谢谢!
您可以使用替换来制作这个:
$('#'+ref).html(textEditorContent.replace(/</g,'<').replace(/>/g, '>'));
或者如果你想保留标签,你可以使用 unescape()
功能。
$(".d").html(unescape($(".d").html()))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="d"><p>Hello World!</p></div>
我正在尝试从来自文本编辑器 #Textarea 的字符串中删除一些特定的 html 标签。 我有一个文本区域,其中包含按钮单击事件。我得到了文本编辑器的所有内容,并想将这些内容分配给另一个 html 标签。但在分配之前,我想删除一些 HTML 元素,或者换句话说,我只想允许 HTML 标签(p,lists,table)。 我正在使用 tinyMCE 文本编辑器,我在按钮点击事件上的功能是
function AssignContents() {
// takes the specific tag ID
var ref=$('.data_button').attr('data_id');
var textEditorContent = tinyMCE.activeEditor.getContent();
$('#'+ref).html(textEditorContent);
}
但是当我在数据库中保存数据时 table 看起来很奇怪。 例如。如果我在文本编辑器 "hello wold" 中编写并使用按钮单击事件,则下面给出的函数将 tinyMCE 的内容分配给 textarea,并在表单提交后将其保存到数据库 table。数据库 table 中的值如下所示。
<p>Hello World!</p>
但我只想 <p>hello world </p>
存储在数据库中而不是其他东西。
谢谢!
您可以使用替换来制作这个:
$('#'+ref).html(textEditorContent.replace(/</g,'<').replace(/>/g, '>'));
或者如果你想保留标签,你可以使用 unescape()
功能。
$(".d").html(unescape($(".d").html()))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="d"><p>Hello World!</p></div>