JavaScript htmlentities 法语
我有一个 .NET MVC 页面,其中包含每个项目的列表 <%: %>
rel 中的编码描述。 我希望能够使用包含我的搜索查询的 rel
搜索所有项目。
其中一个字段的值带有 htmlentities rel='Décoration'
我在搜索框中输入“Décoration”,让 jQuery 搜索具有 'rel' 属性的所有元素,其中包含(indexOf != -1) 该值:
没有结果!
为什么?因为Décoration != Décoration
。
比较这两者的最佳解决方案是什么? (必须适用于所有特殊重音字符,而不仅仅是 é
)
PS(我尝试了两边的 escape/unescape,还尝试了将其附加到 div 的技巧,然后将其读为text,这会替换危险的东西,但不会替换 é (它不必替换,因为无论如何它在 utf-8 中都是有效的))
I have a .NET MVC page with a list of items that each have<%: %>
encoded descriptions in the rel
.
I want to be able to search for all items with a rel
that contains my search query.
One of the fields has a value with htmlentities rel='Décoration'
I type "Décoration" in the search box, let jQuery search for all elements that have a 'rel' attribute that contains (indexOf != -1) that value:
no results!
Why? because Décoration != Décoration
.
What would be the best solution to compare these two? (Has to work for all special accented characters, not just é
)
P.S. (I tried escape/unescape on both sides, also tried the trick to append it to a div and then read it as text, this replaces dangerous stuff, but doesn't replace é (it doesn't have to because it's valid in utf-8 anyway))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
é
之类是html实体,因此可以将乱码字符串设置到临时div的html内容中,并使用元素的文本内容检索解码后的字符串。浏览器将为您完成解码工作。使用 jQuery:
仅使用 DOM:
Since the
é
and like are html entities, you can set the html content of a temporary div with the garbled string, and retrive the decoded string using the text content of the element. The browser will do the decoding work for you.Using jQuery :
Using just the DOM :
如果您使用 UTF-8 编码提供页面,则不需要对所有重音字符使用实体。问题解决了。
If you serve your pages with UTF-8 encoding, you won't need to use entities for all the accented characters. Problem solved.
您可以解码 html 实体。
只需从 此处 复制两个 JavaScript 方法即可
You can decode the html entities.
Just copy the two javascript methods from HERE