确定字符串是否通过jQuery编码为HTML? [英] Determine if string is encoded HTML via jQuery?

查看:142
本文介绍了确定字符串是否通过jQuery编码为HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用验证脚本,但是遇到了一个非常特殊的问题.

I'm working on a validation script, but I'm running into a very particular issue.

如果用户输入的字符串恰好是经过编码的html字符(如&&),则它将作为字符输出(在这种情况下为&).我的问题是:是否可以编写一个确定字符串是否为编码字符的函数?因此,如果用户输入上述两个选项之一,则我想启动一个特定的功能,如果它是非编码字符,则我想做其他事情.

If a user enters a string that happens to be an encoded html character (like & or &), it will output as the character (& in this case). My question is this: is it possible to write a function that detemines if a string is an encoded character? So if the user enters one of the two above options, I want to launch a particular function, and if it's a non-encoded character, I want to do something else.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

通过定义,如果您不知道某物是否为编码的HTML实体,则不知道.您会将来自某个来源的所有文本都视为已编码或未编码.为什么?因为都是文字. & amp;"只是文字.我写&"这里.我不想让任何人解释它,我希望它在字面上显示为& amp;".

By definition, if you do not know whether something is an encoded HTML entity or not you do not know. Either you treat all text coming from a certain source as encoded or not encoded. Why? Because it's all just text. "&" is just text. I meant to write "&" here. I do not want anyone to interpret it, I want it to appear literally as "&".

您怎么知道用户的意思?如果您开始基于猜测来替换用户输入的文本,则在某些情况下,您总是会搞砸它.这是典型的情况,所有的:D"都被图形化的笑脸代替,当您实际上想键入:D"时,这很烦人.

How do you know what the user meant? If you're starting to replace user-entered text based on guesses, you'll always screw it up in some cases. It's the typical case where all ":D" is replaced by a graphical smilie, which is annoying when you actually wanted to type ":D".

如果要始终保留用户输入的内容,请始终通过HTML编码功能运行所有用户输入,该功能将所有特殊字符替换为实体.请参见伟大的逃避现实(或:在文本中使用文本需要了解的知识).

If you want to always preserve exactly what the user entered, always run all user input through an HTML-encoding function which replaces all special characters with entities. See The Great Escapism (Or: What You Need To Know To Work With Text Within Text).

这篇关于确定字符串是否通过jQuery编码为HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆