InnerHTML 在 Firefox 中警告时使用双引号,但在 IE 中则不警告
我制作innerHTML来获取数据并将其存储在数据库中。当我在 Firefox 中尝试时,属性的引号(“或')是正确的。但在 IE 中我没有得到属性的引号(“或')。这是浏览器问题吗?对此有任何答案。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是一个浏览器问题,因为 IE 就是这样做的。请注意,并非 HTML 中的所有属性都必须使用引号,因此这不会使 IE 的输出无效。如果您想要 XHTML,则必须通过遍历 DOM 自行创建它。
Yes, this is a browser issue, in that that's just how IE does it. Note that quotes aren't mandatory for all attributes in HTML, so that doesn't make IE's output invalid. If you want XHTML you'll have to create it yourself by walking the DOM.
是的。当您使用
innerHTML
时,浏览器会根据 DOM 元素和元素中的属性重新创建 HTML。不同的浏览器的做法有所不同,因为直到最近还没有标准(innerHTML
是 Microsoft 的一项创新,几乎被其他所有人采用,并且正在 HTML5)。如果您的标记如下所示:IE 的
innerHTML
将为:...而 Firefox 和 Chrome 与您的原始标记非常接近。
IE 为您提供的是有效的 HTML(您可以省略其中不含空格的属性的引号,并且大写标记名也可以),但不是有效的 XHTML(如果这对您正在做的事情很重要) )。
如果您愿意,您可以通过自己遍历 DOM 树或对 IE 的结果进行后处理来构建自己的 (X)HTML 字符串。
好消息是,现在
innerHTML
是 正在标准化,该标准明确表示结果应该是 XML 文档中的有效 XML(XHTML 文档就是),并且由于 Microsoft 比他们更积极地参与和致力于该领域的标准。多年来,IE9 在这方面可能会做得更好。Yes. When you use
innerHTML
, the browser recreates the HTML based on the DOM elements and attributes in the element. Different browsers do so differently, because until recently there was no standard (innerHTML
was a Microsoft innovation which was adopted by nearly everyone else, and is being standardized in HTML5). If your markup looks like this:IE's
innerHTML
for that will be:...whereas Firefox and Chrome are pretty close to your original.
What IE gives you is valid HTML (you're allowed to omit the quotes on attributes that don't have spaces in them, and UPPER CASE tagnames are okay too), but not valid XHTML (if that's important for what you're doing).
You can build your own (X)HTML string if you like by traversing the DOM tree yourself, or post-process IE's result.
The good news is that now that
innerHTML
is being standardized, and the standard clearly says that the result should be valid XML in an XML document (which XHTML documents are), and since Microsoft is more engaged and dedicated to standards in this area than they've been in years, it's likely that IE9 will do better in this regard.