jQuery html() 和自闭合标签 [英] jQuery html() and self-closing tags

查看:33
本文介绍了jQuery html() 和自闭合标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 jQuery html() 创建自闭合元素时,会发生以下问题:

While creating self-closed elements with jQuery html() the following issue happens:

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

将创建

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

它正确关闭了

  • 标签,但没有关闭

    It closes correctly the <li> tag but not the <input>

    这似乎是 html() 函数中使用的 innerHTML 的问题.

    It seems to be an issue from innerHTML which is used in the html() function.

    我到处寻找并找到了解决方案,但该页面不再可用,如您所见:http://dev.jquery.it/ticket/3378

    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

    有人知道如何解决这个问题吗?

    Anybody knows how to fix this?

    推荐答案

    澄清一下,这是有效的 HTML:

    To clarify, this is valid HTML:

    <input type="checkbox">
    

    这是有效的 XML(包括 XHTML):

    and this is valid XML (including XHTML):

    <input type="checkbox"/>
    

    但它不是有效的 HTML.话虽如此,大多数浏览器可能无论如何都会接受它(但如果这对您有任何意义,文档将无法通过验证).

    but it is not valid HTML. That being said, most browsers will probably accept it anyway (but the document will fail validation if that means anything to you).

    html() 使用 innerHTML.在 IE 和可能的其他浏览器中,这有问题,因为 XHTML 仍然建模为 HTML DOM.请参阅 内部 IE-HTML DOM 仍然不符合 XHTML.

    html() uses innerHTML. In IE and possibly other browsers this has issues because XHTML is still modeled as an HTML DOM. See Internal IE-HTML DOM still isn’t XHTML compliant.

    基本上,几乎没有理由使用 XHTML.这是一个跨浏览器的噩梦.有关原因的详细概要,请参阅 XHTML - 为元素编写自结束标签不是传统上空洞的不良做法吗?,尤其是第一个答案.

    Basically, there is very little reason to use 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?, particularly the first answer.

    这篇关于jQuery html() 和自闭合标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆