简体   繁体   English

jQuery html() 和自闭合标签

[英]jQuery html() and self-closing tags

While creating self-closed elements with jQuery html() the following issue happens:使用 jQuery html() 创建自闭合元素时,会发生以下问题:

$('#someId').html('<li><input type="checkbox" /></li>')

will create会创造

<li><input type="checkbox"></li>

It closes correctly the <li> tag but not the <input>它正确地关闭了<li>标签而不是<input>

It seems to be an issue from innerHTML which is used in the html() function.这似乎是 html() 函数中使用的 innerHTML 的问题。

I have looked everywhere and found a solution for this but the page is not available anymore as you see in: http://dev.jquery.it/ticket/3378我到处寻找并找到了解决方案,但该页面不再可用,如您所见: http : //dev.jquery.it/ticket/3378

Anybody knows how to fix this?有谁知道如何解决这个问题?

To clarify, this is valid HTML:澄清一下,这是有效的 HTML:

<input type="checkbox">

and this is valid XML (including XHTML):这是有效的 XML(包括 XHTML):

<input type="checkbox"/>

but it is not valid HTML.但它不是有效的 HTML。 That being said, most browsers will probably accept it anyway (but the document will fail validation if that means anything to you).话虽如此,大多数浏览器可能无论如何都会接受它(但如果这对您有任何意义,文档将无法通过验证)。

html() uses innerHTML . html()使用innerHTML In IE and possibly other browsers this has issues because XHTML is still modeled as an HTML DOM.在 IE 和可能的其他浏览器中,这有问题,因为 XHTML 仍然建模为 HTML DOM。 See Internal IE-HTML DOM still isn't XHTML compliant .请参阅内部 IE-HTML DOM 仍然不符合 XHTML

Basically, there is very little reason to use XHTML.基本上,几乎没有理由使用 XHTML。 It's a cross browser nightmare.这是一个跨浏览器的噩梦。 For a detailed synopsis as to why see XHTML - Is writing self closing tags for elements not traditionally empty bad practise?有关为什么要查看XHTML - 为元素编写自关闭标签传统上不是空的不良做法的详细概要 , particularly the first answer. ,尤其是第一个答案。

In the era of HTML5, one might argue that <input type="checkbox"> and <input type="checkbox" /> are equally valid representations of the same void element.在 HTML5 时代,人们可能会争辩说<input type="checkbox"><input type="checkbox" />是同一个 void 元素的同样有效的表示。

While that is true, the reason innerHTML still serializes void elements without the /> is twofold:虽然这是真的,但innerHTML仍然在没有/>情况下序列化空元素的原因有两个:

  • A void element is a void element regardless of how you represent it to the browser.一个空元素是一个空元素,不管如何向浏览器表示它。 By the time the browser has constructed the element, its markup is irrelevant, and as far as the DOM is concerned, what it is is an input element of type checkbox.当浏览器构建元素时,它的标记已经无关紧要,就 DOM 而言,它是一个复选框类型的input元素。 The only place the element's "tag" is relevant is its tagName property, and even that has its own quirk .元素的“标签”唯一相关的地方是它的tagName属性, 甚至它也有自己的 quirk

  • There is no reason whatsoever for a browser to begin serializing a void element with the /> syntax when HTML5 itself, by virtue of being based on HTML, not XML, doesn't require it.当 HTML5 本身基于 HTML 而不是 XML 不需要它时,浏览器没有任何理由开始使用/>语法序列化 void 元素。 Doing so just because it's equally valid to use the /> syntax needlessly breaks compatibility with legacy sites for absolutely zero gain (because the presence of the /> doesn't change the meaning of the output in any way).这样做只是因为使用/>语法同样有效,不必要地破坏与遗留站点的兼容性以获得绝对零增益(因为/>存在不会以任何方式改变输出的含义)。 Which brings us back to cletus's answer distinguishing between HTML markup and XHTML markup.这让我们回到了 cletus 的答案,区分了 HTML 标记和 XHTML 标记。

innerHTML , and by extension jQuery.html() , was designed to give you an HTML representation of an element's contents from the DOM . innerHTML以及扩展名jQuery.html()旨在为您提供来自 DOM的元素内容的 HTML 表示。 It is not designed to give you the HTML markup that the browser used to construct the element in the DOM.并非旨在为您提供浏览器用于在 DOM 中构造元素的 HTML 标记。 You cannot "fix" this because there is nothing to fix to begin with.你不能“修复”这个,因为没有什么可以修复的。 What you need to do, is avoid relying on an element's innerHTML for anything other than perhaps the occasional debugging session.您需要做的是,除了偶尔的调试会话之外,避免依赖元素的innerHTML

See also: Nested <p> tag auto-closing/opening另请参阅: 嵌套 <p> 标签自动关闭/打开

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM