需要帮助:jquery prepend doctype to html

car*_*ine 3 html jquery doctype prepend

这是我的情况:

  1. 我正在编辑应用程序的CSS样式表.
  2. 我只能编辑CSS样式表(除非我可以创建性地使用CSS浏览另一个文件,或者可能在现有的.js中添加一个小的jQuery prepend语句)
  3. 申请仅为ie6,ie7和ie8兼容.他们从不使用FireFox,也不是一种选择.

寻求帮助:

1)我认为我需要使用jQuery"prepend/prependTo"一个"doctype"

html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"
Run Code Online (Sandbox Code Playgroud)

如果没有!doctype,它会将ie8抛入quirksmode,当然不接受任何样式,例如"input [type = checkbox]"

我以前没用过prepend.你能帮我解决一下如何制作以下内容的完整而正确的语法:

当前: <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

DESIRED: <doctype html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

这对我来说还没有用 $("html ").prepend("doctype ")

bob*_*nce 24

事实并非如此<doctype html>.它的:

<!DOCTYPE html>
<html (xmlns or any other attributes you want)>
Run Code Online (Sandbox Code Playgroud)

<!DOCTYPE不是一个元素.它<!在开始时对元素无效.这是"doctype 声明 ",在初始解析后无法进行有效修改.

即使在DOM接口允许您移动/替换DocumentType表示doctype声明的节点的浏览器上,这也不具有在Quirks和Standards模式之间进行更改的效果,这是在初始加载时决定的.您不能在模式之间改变文档.

可以从现有文档加载新文档,但更改模式:

<!-- no doctype, loads in Quirks Mode (BackCompat) -->
<html>
    <!-- rest of the document, then at the end: -->

    <script>
        alert('now in compatMode '+document.compatMode);
        if (document.compatMode==='BackCompat') {
            setTimeout(function() {
                var markup= document.documentElement.innerHTML;
                markup= '<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'+markup+'</html>';
                document.open();
                document.write(markup);
                document.close();
            }, 0);
        }
    </script>
</html>
Run Code Online (Sandbox Code Playgroud)

但我强烈建议不要这样做.它很难看,会在加载时间结束时重置任何状态并重绘,并且对脚本编写有各种负面影响.

如果您想要标准模式,您确实需要将doctype添加到HTML本身.如果您无法触摸该应用程序,那么如何使用ISAPI筛选器(假设您的Web服务器是IIS)将doctype添加到其HTML输出中?

  • 非常清晰简洁的信息.您的答案既丰富又易于阅读和理解.您是StackOverflow的重要成员.我正在与公司合作,将DOCTYPE添加到声明中.它已经运行了7年多.希望它可以实现.我不会添加创意脚本来添加DOCTYPE.这将是非常危险的数据被馈送到页面,等等.它需要页面刷新,谁知道会发生什么样的破坏.再次谢谢你! (2认同)