我必须对字符串进行编码,以删除 HTML 元素 ID 的括号。
括号(这些 (
,)
)在 HTML Id 中无效,是否有标准字符串(如 those used in URLs )可供使用?
是否有可以在 ASP.NET MVC 中使用的现有方法?
注意System.Web.Mvc.HttpUtility.HtmlEncode(string)
,不编码括号。
最佳答案
根据 HTML 规范(和 this question about id's ),HTML id
属性中不允许使用括号。如果您需要它们,可以使用字符串替换,例如:
// ( = 'op--' Opening Parenthesis
// ) = 'cp--' Closing Parenthesis
string id = "collectionName.get_Item(index)";
// encode
string encodedId = id.Replace("(", "op--").Replace(")", "cp--");
// decode
string decodedId = encodedId.Replace("op--", "(").Replace("cp--", ")");
关于c# - 用于编码 HTML Id 括号的标准标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6359192/