没有 domready 的 jQuery

发布于 2024-09-06 01:34:16 字数 595 浏览 4 评论 0原文

正在寻找添加此代码的方法, 之后(或某些 )和 之前> 生成(创建)。

$('body').append('<style type="text/css">\
    // some
    // multiline
    // styles
</style>');

没有 domready() 就没有 ,因此使用 head 代替或其他东西似乎更好。 Append也可以替换。

无法直接访问 html 文件编辑,这就是我使用 javascript 的原因。

为什么需要它?当我们使用 domready() 脚本生成页面后添加样式时,因此具有新样式的块从以前的装饰跳转到新的装饰 - 不好,这就是为什么我们应该使用另一个钩子。

有人知道该怎么做吗?

谢谢。

Searching for a way to add this code, after the <head> (or some <link stylesheet>) and before <body> is generated (created).

$('body').append('<style type="text/css">\
    // some
    // multiline
    // styles
</style>');

There is no <body> without domready(), so its seems better to use head instead or something else. Append also can be replaced.

There is no direct access for html file edit, thats why I'm using javascript.

Why is it needed? When we use domready() script adds styles after the page is generated, so blocks with new styles jumps from previous decoration to new - not good, thats why we should use another hook.

Anybody knows how to do that?

Thanks.

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

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

发布评论

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

评论(2

街角迷惘 2024-09-13 01:34:16
<script type="text/javascript">
document.write('<style type="text/css">\
    // some
    // multiline
    // styles
</style>');
</script>

将其放置在您想要的任何地方。

编辑:我认为你想要的不可能,但我很乐意被证明是错误的。您对 DOM 解析阶段没有任何影响。一旦构建了 DOM,对于您的目的来说就已经太晚了。

<script type="text/javascript">
document.write('<style type="text/css">\
    // some
    // multiline
    // styles
</style>');
</script>

Place it wherever you want.

Edit: I don't think what you want is possible, but I'd love to be proven wrong. You don't have any hooks into the DOM parsing stage. And once the DOM is constructed, it's already too late by then for your purposes.

我乃一代侩神 2024-09-13 01:34:16

您是否尝试过 $('body').prepend()$('head').append() ?在使用 jquery 注入 html 之前,您需要准备好 DOM,所以我认为您必须准备好文档。

Did you try $('body').prepend() or $('head').append() ? You need the DOM to be ready before you can inject html using jquery, so I think you're stuck with document ready.

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