有没有一种简单的方法可以在不重新加载页面的情况下重新加载CSS?
我正在尝试制作一个带有预览功能的实时页内 css 编辑器,该编辑器可以重新加载样式表并应用它,而无需重新加载页面。解决这个问题的最佳方法是什么?
I'm trying to make a live, in-page css editor with a preview function that would reload the stylesheet and apply it without needing to reload the page. What would be the best way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
可能不适用于您的情况,但这是我用于重新加载外部样式表的 jQuery 函数:
Possibly not applicable for your situation, but here's the jQuery function I use for reloading external stylesheets:
在“编辑”页面上,不要以正常方式包含 CSS(使用
标记),而是将其全部写入
标记。编辑其
innerHTML
属性将自动更新页面,即使无需往返服务器。Javascript,使用 jQuery:
应该就是这样了!
如果您想要真正的奇特,请将该功能附加到文本区域的按键上,尽管您可能会得到一些不需要的副作用(页面会在您键入时不断变化)
编辑:经过测试并且有效(至少在 Firefox 3.5 中是这样,但其他浏览器应该没问题)。请参阅此处的演示:http://jsbin.com/owapi
On the "edit" page, instead of including your CSS in the normal way (with a
<link>
tag), write it all to a<style>
tag. Editing theinnerHTML
property of that will automatically update the page, even without a round-trip to the server.The Javascript, using jQuery:
And that should be it!
If you wanted to be really fancy, attach the function to the keydown on the textarea, though you could get some unwanted side-effects (the page would be changing constantly as you type)
Edit: tested and works (in Firefox 3.5, at least, though should be fine with other browsers). See demo here: http://jsbin.com/owapi
完全没有必要为此使用 jQuery。以下 JavaScript 函数将重新加载所有 CSS 文件:
There is absolutely no need to use jQuery for this. The following JavaScript function will reload all your CSS files:
Vanilla JS 中的一个较短版本,一行:
它循环遍历所有样式表链接,并将时间戳附加(或更新)到 URL。
A shorter version in Vanilla JS and in one line:
It loops trough all stylesheet links and appends (or updates) a timestamp to the URL.
查看 Andrew Davey 的时尚 Vogue 项目 - http://aboutcode.net/vogue/
Check out Andrew Davey's snazzy Vogue project - http://aboutcode.net/vogue/
另一种 jQuery 解决方案
对于 id 为“css”的单个样式表,请尝试以下操作:
将其包装在具有全局作用域的函数中,您可以从 Chrome 中的开发者控制台或 Firefox 中的 Firebug 使用它:
One more jQuery solution
For a single stylesheet with id "css" try this:
Wrap it in a function that has global scrope and you can use it from the Developer Console in Chrome or Firebug in Firefox:
基于以前的解决方案,我使用 JavaScript 代码创建了书签:
来自 Firefox 的图像:
它有什么作用?
它通过添加查询字符串参数来重新加载 CSS(如上面的解决方案):
Based on previous solutions, I have created bookmark with JavaScript code:
Image from Firefox:
What does it do?
It reloads CSS by adding query string params (as solutions above):
由于这个问题在 2019 年的 stackoverflow 中出现过,我想使用更现代的 JavaScript 来添加我的贡献。
具体来说,对于非内联的 CSS 样式表 - 因为这已经在原始问题中以某种方式涵盖了。
首先,请注意,我们仍然没有可构造样式表对象。不过,我们希望它们能够尽快落地。
同时,假设有以下 HTML 内容:
我们可以在
index.js
中拥有:Since this question was shown in the stackoverflow in 2019, I'd like to add my contribution using a more modern JavaScript.
Specifically, for CSS Stylesheet that are not inline – since that is already covered from the original question, somehow.
First of all, notice that we still don't have Constructable Stylesheet Objects. However, we hope to have them landed soon.
In the meantime, assuming the following HTML content:
We could have, in
index.js
:我现在有这个:
在页面中使用 id changeCss 制作任何元素,并使用 href 属性,其中包含新的 css url。和一个带有起始 css 的链接元素:
i now have this:
make any element in your page with id changeCss with a href attribute with the new css url in it. and a link element with the starting css:
另一个答案:
有一个名为 ReCSS 的书签。我没有广泛使用它,但似乎有效。
该页面上有一个书签,可以拖放到地址栏上(这里似乎无法制作)。如果它坏了,代码如下:
Another answer:
There's a bookmarklet called ReCSS. I haven't used it extensively, but seems to work.
There's a bookmarklet on that page to drag and drop onto your address bar (Can't seem to make one here). In case that's broke, here's the code:
您可以以简单的方式使用 rel="preload" 而不是 rel="stylesheet" 。
In a simple manner you can use rel="preload" instead of rel="stylesheet" .
基于此页面上的 https://stackoverflow.com/a/59176853/3917091
这是一种快速更改 css 进行测试的好方法,无需始终刷新 ctrl F5,然后通过服务器端更新文件名或查询来推出更改样式表上的字符串正常。
data-href“保留”很重要,或者永久刷新缓存只能在每个页面视图中起作用一次。
可以为 script[src] 编写相同类型的脚本,但我不建议这样做,因为某些事情执行两次可能会出现各种错误,例如事件侦听器。
Based on https://stackoverflow.com/a/59176853/3917091 on this page
It's a nice way to quickly change css for testing without ctrl F5 refresh all of the time, and then you roll out the change by server-side updating the filename or the query string on the style sheet as normal.
The data-href 'preserving' is important, or the permanency of refreshing on your cache only works once per page-view.
The same sort of script can be written for script[src] but I would not advise that, because you may end up with all kinds of bugs with certain things being done twice, like event listeners.
是的,重新加载 css 标签。并记住使新的 url 唯一(通常通过附加随机查询参数)。我有代码可以做到这一点,但现在还没有。稍后编辑...
编辑:太晚了...harto 和 nickf 打败了我;-)
Yes, reload the css tag. And remember to make the new url unique (usually by appending a random query parameter). I have code to do this but not with me right now. Will edit later...
edit: too late... harto and nickf beat me to it ;-)
如果你使用 php 就很简单
只需在 css 末尾附加当前时间,就像
现在每次你重新加载无论它是什么时,时间都会发生变化,浏览器会认为它是一个不同的文件,因为最后一位不断变化......你可以对你强制的任何文件执行此操作浏览器始终使用您想要的任何脚本语言进行刷新
simple if u are using php
Just append the current time at the end of the css like
So now everytime u reload whatever it is , the time changes and browser thinks its a different file since the last bit keeps changing.... U can do this for any file u force the browser to always refresh using whatever scripting language u want