是否有一个 JavaScript 函数可以将字符转换为 &code;相等的?
我有使用 CKeditor 创建的文本,它似乎在应有空格的位置插入
。它似乎对 >、<、& 等进行了类似的转换,这很好,除了当我进行 DOMSelection 时,这些代码被删除。
所以,这就是选择的内容:
before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a> (2)
但这就是 DOM 中实际的内容:
before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a> (2)
请注意,我使用variable.inspect输出了选择内容和存储在数据库中的原始文本,因此所有引号都被转义了(当发送到浏览器)。
为了避免每个人寻找差异的痛苦:
从第一个开始:孵化场 (2)
(选择)
从第二个开始:Hatchery (2)
(原始)
这些差异是在选择的最后。
所以...我认为可以通过三种方法来解决这个问题。
1) - Replace all characters commonly replaced with codes with their codes,
and hope for the best.
2) - Javascript may have some uncommon function / a library may exist that
replaces these characters for me (I think this might be the way CKeditor
does its character conversion).
3) - Figure out the way CKeditor converts and do the conversion exactly that way.
我正在使用 Ruby on Rails,但这对于这个问题来说应该不重要。
我发现它可以转换的其他一些东西:
1: It seems to only convert spaces to if the space(s) is before or after a tag:
e.g.: "With quick <a href..."
2: It changes apostrophes to the hex value
e.g.: "opponent's"
3: It changes "&" to "&"
4: It changes angle brackets to ">" and "<" appropriately.
有人对此有什么想法吗?
I have text that was created created with CKeditor, and it seems to be inserting
where spaces should be. And it appears to do similar conversions with >, <, &, etc. which is fine, except, when I make a DOMSelection, those codes are removed.
So, this is what is selected:
before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a> (2)
But this is what is actually in the DOM:
before<a href=\"http://wiki.teamliquid.net/starcraft2/Hatchery\" style=\"text-decoration: none; color: rgb(0, 43, 184); background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial; \" title=\"Hatchery\">Hatchery</a> (2)
note that I outputted the selection and the original text stored in the database using variable.inspect, so all the quotes are escaped (they wouldn't be when sent to the browser).
To save everyone the pain of looking for the difference:
From the first: Hatchery</a> (2)
(The Selection)
From the second: Hatchery</a> (2)
(The original)
These differences are at the very end of the selection.
So... there are three ways, I can see of, to approach this.
1) - Replace all characters commonly replaced with codes with their codes,
and hope for the best.
2) - Javascript may have some uncommon function / a library may exist that
replaces these characters for me (I think this might be the way CKeditor
does its character conversion).
3) - Figure out the way CKeditor converts and do the conversion exactly that way.
I'm using Ruby on Rails, but that shouldn't matter for this problem.
Some other things that I found out that it converts:
1: It seems to only convert spaces to if the space(s) is before or after a tag:
e.g.: "With quick <a href..."
2: It changes apostrophes to the hex value
e.g.: "opponent's"
3: It changes "&" to "&"
4: It changes angle brackets to ">" and "<" appropriately.
Does anyone have any thoughts on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
str
中编码 html 实体(如果我理解正确的话,您的问题标题要求这样做):在
str
中解码 html 实体:这些依赖于 jQuery,但普通的替代方案是基本相同但更详细。
对
str
中的 html 实体进行编码:对
str
中的 html 实体进行解码:To encode html entities in
str
(your question title asks for this, if I understand correctly):To decode html entities in
str
:These rely on jQuery, but vanilla alternatives are basically the same but more verbose.
To encode html entities in
str
:To decode html entities in
str
: