HTML属性可以有撇号吗
Posted
技术标签:
【中文标题】HTML属性可以有撇号吗【英文标题】:Can HTML attributes have apostrophes 【发布时间】:2011-12-25 21:27:38 【问题描述】:由于多种原因,我最终得到了一些需要在 html 属性标签内存储 JSON 序列化对象的代码。
然而,我遇到了一个问题,我的 json 序列化对象之一包含撇号,因此似乎破坏了 jQuery。
基本上这就是行为......
这是表头定义。
<table class='DraggableRowTable' RowData='"AuthorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Created":"\/Date(1319429903223+1100)\/","EditorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Modified":"\/Date(1319429903223+1100)\/","AssignedToID":"7daef058-d84c-43f5-9db1-8011c59673ee","DetailedTaskID":"169ea939-7cb7-4599-9c2a-9e8ef4dee641","Duration":4.0000,"EndDate":"\/Date(1319497200000+1100)\/","ManualCalculation":false,"RoleID":"944f0ea3-4312-4f14-9d00-c968a647996a","Sequence":14,"StartDate":"\/Date(1319429872333+1100)\/","Status":0,"SubSequence":0,"Task":"AuthorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Created":"\/Date(1319172394650+1100)\/","EditorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Modified":"\/Date(1319172394650+1100)\/","AssignedToID":"7daef058-d84c-43f5-9db1-8011c59673ee","Duration":4,"IsActive":true,"ProductID":"dad82b06-7d28-4255-bf02-f709eeed64cd","RoleID":"944f0ea3-4312-4f14-9d00-c968a647996a","Sequence":14,"TaskDescription":"es reviewer is to review the following fields and provide feedback to the writer if they' need fixing:\u000d\u000a*description\u000d\u000a*key learning objectives\u000d\u000a*educational value statements","TaskID":"6f577537-ce4a-46cd-9eb6-29bd5756ab26","TaskName":"educational soundness review","TaskID":"6f577537-ce4a-46cd-9eb6-29bd5756ab26","WorkOrder":"AuthorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Created":"\/Date(1319429872333+1100)\/","EditorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Modified":"\/Date(1319429872333+1100)\/","IsActive":true,"Priority":1,"ProductID":"dad82b06-7d28-4255-bf02-f709eeed64cd","Qty":1,"StartDate":"\/Date(1319429872333+1100)\/","Status":0,"Subtitle":null,"WorkOrderDescription":null,"WorkOrderExternalID":43,"WorkOrderID":"66c40ff8-56e1-4009-89ab-989b0e25d120","WorkOrderName":"Work order #43","WorkOrderID":"66c40ff8-56e1-4009-89ab-989b0e25d120"'>
这是代码。
var Data = $(GridRow).find(".DraggableRowTable").attr("RowData");
//Data = ""AuthorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Created":"\/Date(1319429903223+1100)\/","EditorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Modified":"\/Date(1319429903223+1100)\/","AssignedToID":"7daef058-d84c-43f5-9db1-8011c59673ee","DetailedTaskID":"169ea939-7cb7-4599-9c2a-9e8ef4dee641","Duration":4.0000,"EndDate":"\/Date(1319497200000+1100)\/","ManualCalculation":false,"RoleID":"944f0ea3-4312-4f14-9d00-c968a647996a","Sequence":14,"StartDate":"\/Date(1319429872333+1100)\/","Status":0,"SubSequence":0,"Task":"AuthorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Created":"\/Date(1319172394650+1100)\/","EditorID":"652c90e4-ffef-49c5-9217-0ff39a461011","Modified":"\/Date(1319172394650+1100)\/","AssignedToID":"7daef058-d84c-43f5-9db1-8011c59673ee","Duration":4,"IsActive":true,"ProductID":"dad82b06-7d28-4255-bf02-f709eeed64cd","RoleID":"944f0ea3-4312-4f14-9d00-c968a647996a","Sequence":14,"TaskDescription":"es reviewer is to review the following fields and provide feedback to the writer if they" //As you can see, it all breaks here...
所以我想我想知道,首先在 HTML 属性中使用撇号是否有效(IE:HTML 标准是否支持这一点),其次(取决于对第一个的答案)jQuery 是否应该能够正确返回此属性?
【问题讨论】:
哇,在很多层面上都错了。我简直无语了。 我讨厌这样的 cmets。而不是仅仅指出有问题的地方,也许您想实际转发一些建设性的东西? 【参考方案1】:你没有说哪个版本的 HTML,所以我假设 HTML5
除非另有说明,HTML 元素的属性可以有任何字符串值,包括空字符串。除非明确说明,对这些属性中可以指定的文本没有任何限制。
您的问题是您没有编码撇号。您可能需要将出现的'
更改为&apos;
。你真的应该对整个字符串进行 HTML 编码。
【讨论】:
我接受您关于 JSON 应该被编码的批评,但是如果标准允许撇号(并且您的段落表明它允许)这是否意味着 jQuery 实现中存在错误? 你有整个标签属性在单引号中;嵌入的单引号必须以某种方式转义,这就是您在 HTML 中使用实体的方式。或者,您可以将属性值放在双引号中,然后实体转义所有双引号,我想。以上是关于HTML属性可以有撇号吗的主要内容,如果未能解决你的问题,请参考以下文章
单元格格式为文本或前面有撇号,SAP Webi 中生成的 Excel 文件