需要帮助:jquery prepend doctype to html

发布于 2024-10-08 05:07:07 字数 732 浏览 7 评论 0原文

这是我的情况:

  1. 我正在编辑应用程序的 CSS 样式表。
  2. 我只能编辑 CSS 样式表(除非我可以使用 CSS 创造性地连接到另一个文件,或者可能在现有的 .js 中添加一个小的 jQuery 前置语句)
  3. 应用程序仅兼容 ie6、ie7 和 ie8。他们从不使用 FireFox,这也不是一个选择。

寻求帮助:

1)我想我需要使用 jQuery 来“prepend/prependTo”一个“doctype”,如果

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

没有!doctype,它会将 ie8 抛入怪异模式,当然不接受任何诸如“input[type=checkbox]”之类的样式

我以前没有使用过前置。您能否帮助我使用完整且正确的语法来制作以下内容:

CURRENT:

所需:< /code>

这对我来说还不起作用 $("html ").prepend("doctype ")

Here's my situation:

  1. I am editing an application's CSS style sheet.
  2. I can ONLY edit the CSS style sheet (unless I can creatively glom onto another file using the CSS, or possibly add a small jQuery prepend statement in an existing .js)
  3. Application is ONLY ie6, ie7 and ie8 compliant. They never use FireFox, and it's not an option.

Looking for help with:

1) I think I need to use jQuery to "prepend/prependTo" a "doctype " on to

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

Without the !doctype it throws ie8 into quirksmode and of course doesn't accept any styles such as "input[type=checkbox]"

I have not used prepend before. Can you help me with full and correct syntax on how to make the following:

CURRENT: <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">

This has not worked for me yet $("html ").prepend("doctype ")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2024-10-15 05:07:07

它不是 。它是:

<!DOCTYPE html>
<html (xmlns or any other attributes you want)>

不是一个元素。它的开头有 ,这对于元素来说是无效的。这是“doctype 声明”,在初始解析后无法对其进行有效更改。

即使在 DOM 接口允许您移动/替换表示 doctype 声明的 DocumentType 节点的浏览器上,这也不会产生在 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>

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

如果您想要标准模式,您确实需要将文档类型添加到 HTML 本身。如果您绝对无法接触应用程序,那么使用 ISAPI 过滤器(假设您的 Web 服务器是 IIS)将 doctype 添加到其 HTML 输出怎么样?

It's not <doctype html>. It's:

<!DOCTYPE html>
<html (xmlns or any other attributes you want)>

<!DOCTYPE is not an element. It has <! at the start, which is invalid for an element. This is the the “doctype declaration”, and it cannot usefully be altered after initial parsing.

Even on browsers whose DOM interfaces let you move/replace the DocumentType node representing the doctype declaration, this does not have the effect of changing between Quirks and Standards mode, which is something that is decided only at initial load time. You cannot mutate a document between modes.

You can load a new document from the existing document but with a changed mode:

<!-- 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>

But I would strongly advise against it. It's ugly, will reset any state and redraw at the end of load time, and has all sorts of negative implications for scripting.

If you want Standards Mode, you really need to be adding the doctype to the HTML itself. If you absolutely can't touch the application, how about using an ISAPI filter (assuming your web server is IIS) to add the doctype to its HTML output?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文