需要 Pure/jQuery Javascript 解决方案来清除文本区域中的 Word HTML

发布于 2024-07-25 18:54:47 字数 428 浏览 3 评论 0原文

我知道这个问题已经在这里讨论过,但我还没有找到适合我的情况的可行解决方案,所以我想让智囊团重新开始工作,看看能做些什么。

我有一个表单中的文本区域,需要检测何时将某些内容粘贴到其中,并清除任何隐藏的 HTML 和 HTML 内容。 引号。 此表单的内容通过电子邮件发送到第三方系统,该系统特别棘手,因此有时甚至将其编码为 html 实体字符也不是一个安全的选择。

不幸的是,我不能使用 FCKEditor、TinyMCE 等,在这种情况下它必须保持常规文本区域。 我试图从 word 函数中剖析 FCKEditor 的粘贴,但没有运气找到它。

不过,如果需要的话,我可以使用 jQuery 库,但还没有为此找到 jQuery 插件。

我专门寻找旨在清理粘贴的信息的信息,而不是如何监视元素的内容更改。

任何建设性的帮助将不胜感激。

I know this issue has been touched on here but I have not found a viable solution for my situation yet, so I'd like to but the brain trust back to work and see what can be done.

I have a textarea in a form that needs to detect when something is pasted into it, and clean out any hidden HTML & quotation marks. The content of this form is getting emailed to a 3rd party system which is particularly bitchy, so sometimes even encoding it to the html entity characters isn't going to be a safe bet.

I unfortunately cannot use something like FCKEditor, TinyMCE, etc, it's gotta stay a regular textarea in this instance. I have attempted to dissect FCKEditor's paste from word function but have not had luck tracking it down.

I am however able to use the jQuery library if need be, but haven't found a jQuery plugin for this just yet.

I am specifically looking for information geared towards cleaning the information pasted in, not how to monitor the element for change of content.

Any constructive help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

打小就很酷 2024-08-01 18:54:47

我正在看大卫·阿彻的回答,他几乎回答了这个问题。 我过去使用过与他类似的解决方案:

$("textarea").change( function() {
    // convert any opening and closing braces to their HTML encoded equivalent.
    var strClean = $(this).val().replace(/</gi, '<').replace(/>/gi, '>');

    // Remove any double and single quotation marks.
    strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');

    // put the data back in.
    $(this).val(strClean);
});

如果您正在寻找一种完全删除 HTML 标签的方法

$("textarea").change( function() {
    // Completely strips tags.  Taken from Prototype library.
    var strClean = $(this).val().replace(/<\/?[^>]+>/gi, '');

    // Remove any double and single quotation marks.
    strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');

    // put the data back in.
    $(this).val(strClean);
});

I am looking at David Archer's answer and he pretty much answers it. I have used in the past a solution similar to his:

$("textarea").change( function() {
    // convert any opening and closing braces to their HTML encoded equivalent.
    var strClean = $(this).val().replace(/</gi, '<').replace(/>/gi, '>');

    // Remove any double and single quotation marks.
    strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');

    // put the data back in.
    $(this).val(strClean);
});

If you are looking for a way to completely REMOVE HTML tags

$("textarea").change( function() {
    // Completely strips tags.  Taken from Prototype library.
    var strClean = $(this).val().replace(/<\/?[^>]+>/gi, '');

    // Remove any double and single quotation marks.
    strClean = strClean.replace(/"/gi, '').replace(/'/gi, '');

    // put the data back in.
    $(this).val(strClean);
});
别念他 2024-08-01 18:54:47

您可以查看 Connor McKay 的 Word HTML Cleaner。 它是一种非常强大的清洁剂,因为它可以清除很多您可能想要保留的东西,但如果这不是问题,它看起来相当不错。

You could check out Word HTML Cleaner by Connor McKay. It is a pretty strong cleaner, in that it removes a lot of stuff that you might want to keep, but if that's not a problem it looks pretty decent.

谢绝鈎搭 2024-08-01 18:54:47

像这样的事情怎么样:

function cleanHTML(pastedString) {
    var cleanString = "";
    var insideTag = false;
    for (var i = 0, var len = pastedString.length; i < len; i++) {
        if (pastedString.charAt(i) == "<") insideTag = true;
        if (pastedString.charAt(i) == ">") {
            if (pastedString.charAt(i+1) != "<") {
                insideTag = false;
                i++;
            }
        }
        if (!insideTag) cleanString += pastedString.charAt(i);
    }
    return cleanString;
}

然后只需使用事件侦听器来调用此函数并传入粘贴的字符串即可。

What about something like this:

function cleanHTML(pastedString) {
    var cleanString = "";
    var insideTag = false;
    for (var i = 0, var len = pastedString.length; i < len; i++) {
        if (pastedString.charAt(i) == "<") insideTag = true;
        if (pastedString.charAt(i) == ">") {
            if (pastedString.charAt(i+1) != "<") {
                insideTag = false;
                i++;
            }
        }
        if (!insideTag) cleanString += pastedString.charAt(i);
    }
    return cleanString;
}

Then just use the event listener to call this function and pass in the pasted string.

你的笑 2024-08-01 18:54:47

使用模糊事件可能会很有用,因为该事件触发频率较低:

$("textarea").blur(function() {
    // check input ($(this).val()) for validity here
});

It might be useful to use the blur event which would be triggered less often:

$("textarea").blur(function() {
    // check input ($(this).val()) for validity here
});
少女七分熟 2024-08-01 18:54:47

从 jquery 文档编辑。

$("textarea").change( function() {
    // check input ($(this).val()) for validity here
});

这是为了检测更改。 clean 可能是

上面编辑的某种正则表达式,用于查找文本区域而不是文本框

Edited from the jquery docs..

$("textarea").change( function() {
    // check input ($(this).val()) for validity here
});

Thats for detecting the changes. The clean would probably be a regex of sorts

edited above to look for a textarea not a textbox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文