我可以根据要求加载外部样式表吗?

发布于 2024-08-19 07:57:39 字数 234 浏览 6 评论 0原文

$.getScript('ajax/test.js', function() {
  alert('Load was performed.');
});

.. 就像上面根据请求加载外部 JS 的代码一样,是否有类似的东西可以在需要时加载外部 CSS 样式表?

例如,当我在网站上使用灯箱(内联弹出窗口)时,我希望避免加载灯箱 JS 和 CSS 文件,除非用户请求。

谢谢

$.getScript('ajax/test.js', function() {
  alert('Load was performed.');
});

.. like the above code which loads an external JS on request, is there something similar available to load an external CSS stylesheet when required?

Like for example when I use lightboxes (inline popups) on my site, I want to avoid loading lightbox JS and CSS files onload, unless requested by the user.

Thanks

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

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

发布评论

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

评论(7

夜深人未静 2024-08-26 07:57:40

你可以这样做:

$('<link>').attr('rel','stylesheet')
  .attr('type','text/css')
  .attr('href','link_to.css')
  .appendTo('head');

You could do:

$('<link>').attr('rel','stylesheet')
  .attr('type','text/css')
  .attr('href','link_to.css')
  .appendTo('head');
两相知 2024-08-26 07:57:40

您只需在 head 中添加动态 HTML 内容即可添加对外部样式表的引用:

$('head').append('<link rel="stylesheet" href="style.css" type="text/css" />');

此内容将插入到 DOM 中,然后正常呈现,在这种情况下会导致获取外部样式表。

不过,也要注意 cletus 的回答,如果您不小心动态加载静态内容,最终可能会花费您的页面加载时间和过多的资源传输。

You can add references to external stylesheets simply by adding dynamic HTML content in the head:

$('head').append('<link rel="stylesheet" href="style.css" type="text/css" />');

This content gets inserted in the DOM, and subsequently rendered normally, in this case causing a fetch of an external stylesheet.

Pay attention to cletus' answer as well however, as if you're not careful with dynamic loading of static content, it can end up costing you in page load time and excessive resource transfer.

执手闯天涯 2024-08-26 07:57:40

一般来说,如果你做得正确的话,最好只包含你需要的所有内容。

我的意思是,静态内容(图像、Javascript、CSS)的最佳实践是:

  • 最小化外部 HTTP 请求(最小化文件数量);
  • 对文件进行版本控制并使用远期的 Expires 标头;
  • 适当缩小和压缩内容。

因此用户只会下载给定文件一次,直到它发生更改。

动态加载 CSS 和 Javascript,除非特别大,否则通常是没有根据的并且会适得其反。

Generally you're better off just including all you need assuming you're doing so correctly.

By that I mean that best practice for static content (images, Javascript, CSS) is to:

  • minimize external HTTP requests (minimize # of files);
  • version the files and use a far futures Expires header;
  • minify and compress content as appropriate.

so a user will only ever download a given file once until it changes.

Dynamically loading CSS and Javascript, unless it is exceptionally large, is typically unwarranted and counterproductive.

高冷爸爸 2024-08-26 07:57:40

IE 问题...

我使 IE 6、7、8 崩溃,只需

$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'assets/css/jquery.fancybox-1.3.4.css') );

将它们复制到主表中以避免另一个 http 请求,瞧。

IE issues...

I was crashing IE 6,7,8 with

$('head').append( $('<link rel="stylesheet" type="text/css" />').attr('href', 'assets/css/jquery.fancybox-1.3.4.css') );

Just copied them into the main sheet to avoid another http req, voila.

撩人痒 2024-08-26 07:57:40

我们可以直接附加它

$("head").append($("<link rel='stylesheet' href='style.css' type='text/css' media='screen' />"));

We can append it directly by

$("head").append($("<link rel='stylesheet' href='style.css' type='text/css' media='screen' />"));
时光磨忆 2024-08-26 07:57:39

是的:如果您创建链接到样式表的 标记并将其添加到 标记,浏览器将加载该样式表。

例如

$('head').append('<link rel="stylesheet" type="text/css" href="lightbox_stylesheet.css">');

但是根据@peteorpeter的评论,这在 IE 8 或更低版本中不起作用 - 在那里,您需要:

  1. 在设置其 href>;或者
  2. 使用 IE 的 document.createStyleSheet() 方法

此外,检查链接是否已添加并不能在所有浏览器中可靠地工作。

我也会质疑你的部分前提:

我想避免在加载时加载灯箱 JS 和 CSS 文件,除非用户请求。

为什么?减少页面重量?我可以理解这种愿望,但你应该测量 CSS 和 JS 文件的大小(缩小和压缩后),其中是否包含灯箱代码,看看减少是否值得:

  1. 按需加载增加的复杂性; 灯箱的响应能力略有
  2. 下降(因为在按需加载时,灯箱必须等待自己的 CSS 和 JS 加载才能完成其任务)

在缩小和 gzip 压缩之后,很可能不会有太大差异。

请记住,您可以指示浏览器长时间缓存您的 CSS 和 JS,这意味着只有当用户访问您的缓存为空的网站时才会下载它。

Yup: if you create a <link> tag linking to a stylesheet and add it to the <head> tag, the browser will load that stylesheet.

E.g.

$('head').append('<link rel="stylesheet" type="text/css" href="lightbox_stylesheet.css">');

However, as per @peteorpeter’s comments, this doesn’t work in IE 8 or under — there, you need to either:

  1. append the <link> before setting its href; or
  2. use IE’s document.createStyleSheet() method

Also, checking whether a link has already been added doesn’t work reliably across all browsers.

I would also question part of your premise:

I want to avoid loading lightbox JS and CSS files onload, unless requested by the user.

Why? To reduce page weight? I can understand the desire, but you should measure the size of your CSS and JS files (after minification and gzipping) with the lightbox code in there, and without, to see whether the reduction is worth:

  1. the added complexity of loading on-demand; and
  2. the slightly reduced responsiveness of the lightbox (because when loading on-demand, the lightbox will have to wait for its own CSS and JS to load before it can do its thing)

After minification and gzipping, there may well not be that much difference.

And bear in mind that you can instruct the browser to cache your CSS and JS for a long time, meaning it only gets downloaded when a user visits your site with an empty cache.

束缚m 2024-08-26 07:57:39
$('<link/>', {rel: 'stylesheet', href: 'myHref'}).appendTo('head');
$('<link/>', {rel: 'stylesheet', href: 'myHref'}).appendTo('head');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文