我正在尝试制作一个实时的、在页面内的CSS编辑器,并具有预览功能,可以重新加载样式表并应用它而无需重新加载整个页面。那么,最好的方法是什么?
我正在尝试制作一个实时的、在页面内的CSS编辑器,并具有预览功能,可以重新加载样式表并应用它而无需重新加载整个页面。那么,最好的方法是什么?
也许不适用于您的情况,但这是我用于重新加载外部样式表的 jQuery 函数:
/**
* Forces a reload of all stylesheets by appending a unique query string
* to each stylesheet URL.
*/
function reloadStylesheets() {
var queryString = '?reload=' + new Date().getTime();
$('link[rel="stylesheet"]').each(function () {
this.href = this.href.replace(/\?.*|$/, queryString);
});
}
var links = document.getElementsByTagName("link"); for (var i = 0; i < links.length;i++) { var link = links[i]; if (link.rel === "stylesheet") {link.href += "?"; }}
- Claude在“编辑”页面上,不要使用普通的<link>
标签来包含你的CSS样式,而是将所有内容都写到<style>
标签中。通过编辑该标签的innerHTML
属性,即可自动更新页面,甚至无需返回服务器进行往返请求。
<style type="text/css" id="styles">
p {
color: #f0f;
}
</style>
<textarea id="editor"></textarea>
<button id="preview">Preview</button>
使用jQuery编写的Javascript代码:
jQuery(function($) {
var $ed = $('#editor')
, $style = $('#styles')
, $button = $('#preview')
;
$ed.val($style.html());
$button.click(function() {
$style.html($ed.val());
return false;
});
});
就这些了!
如果你想要更高级一点,可以将函数附加到文本区域的keydown事件上,但是可能会出现一些意外的副作用(在您键入时页面会不断变化)。
编辑:已测试并可行(至少在Firefox 3.5中可以,其他浏览器也应该没问题)。在这里查看演示:http://jsbin.com/owapi
<style>
пјҲе’Ң<script>
пјүе…ғзҙ зҡ„innerHTML
гҖӮ - JPot毫无必要使用jQuery来实现这个功能。以下JavaScript函数将重新加载您的所有CSS文件:
function reloadCss()
{
var links = document.getElementsByTagName("link");
for (var cl in links)
{
var link = links[cl];
if (link.rel === "stylesheet")
link.href += "";
}
}
rel
属性值可以是 "stylesheet",但它的类型不是字符串? - Geat用 Vanilla JS 缩短并压缩成一行:
document.querySelectorAll("link[rel=stylesheet]").forEach(link => link.href = link.href.replace(/\?.*|$/, "?" + Date.now()))
它循环遍历所有样式表链接,并向URL附加(或更新)时间戳。
看看Andrew Davey的时尚Vogue项目 - http://aboutcode.net/vogue/
再来一个jQuery解决方案
针对具有id“css”的单一样式表,请尝试以下方法:
$('#css').replaceWith('<link id="css" rel="stylesheet" href="css/main.css?t=' + Date.now() + '"></link>');
将其包装在具有全局范围的函数中,您可以在Chrome的开发者控制台或Firefox的Firebug中使用它:
var reloadCSS = function() {
$('#css').replaceWith('<link id="css" rel="stylesheet" href="css/main.css?t=' + Date.now() + '"></link>');
};
基于之前的解决方案,我已经用JavaScript代码创建了书签:
javascript: { var toAppend = "trvhpqi=" + (new Date()).getTime(); var links = document.getElementsByTagName("link"); for (var i = 0; i < links.length;i++) { var link = links[i]; if (link.rel === "stylesheet") { if (link.href.indexOf("?") === -1) { link.href += "?" + toAppend; } else { if (link.href.indexOf("trvhpqi") === -1) { link.href += "&" + toAppend; } else { link.href = link.href.replace(/trvhpqi=\d{13}/, toAppend)} }; } } }; void(0);
来自Firefox的图片:
它的作用是什么?
通过添加查询字符串参数(如上述解决方案),重新加载CSS:
自2019年这个问题在stackoverflow上出现以来,我想使用更现代的JavaScript来做出贡献。
具体来说,针对不是内联的CSS样式表——因为原始问题已经涵盖了这一点,某种程度上。
首先,请注意我们仍然没有可构建的StyleSheet对象。 但是,我们希望很快就能拥有它们。
与此同时,假设以下HTML内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link id="theme" rel="stylesheet" type="text/css" href="./index.css" />
<script src="./index.js"></script>
</head>
<body>
<p>Hello World</p>
<button onclick="reload('theme')">Reload</button>
</body>
</html>
index.js
文件中实现以下功能:// Utility function to generate a promise that is
// resolved when the `target` resource is loaded,
// and rejected if it fails to load.
//
const load = target =>
new Promise((rs, rj) => {
target.addEventListener("load", rs, { once: true });
target.addEventListener(
"error",
rj.bind(null, `Can't load ${target.href}`),
{ once: true }
);
});
// Here the reload function called by the button.
// It takes an `id` of the stylesheet that needs to be reloaded
async function reload(id) {
const link = document.getElementById(id);
if (!link || !link.href) {
throw new Error(`Can't reload '${id}', element or href attribute missing.`);
}
// Here the relevant part.
// We're fetching the stylesheet from the server, specifying `reload`
// as cache setting, since that is our intention.
// With `reload`, the browser fetches the resource *without* first looking
// in the cache, but then will update the cache with the downloaded resource.
// So any other pages that request the same file and hit the cache first,
// will use the updated version instead of the old ones.
let response = await fetch(link.href, { cache: "reload" });
// Once we fetched the stylesheet and replaced in the cache,
// We want also to replace it in the document, so we're
// creating a URL from the response's blob:
let url = await URL.createObjectURL(await response.blob());
// Then, we create another `<link>` element to display the updated style,
// linked to the original one; but only if we didn't create previously:
let updated = document.querySelector(`[data-link-id=${id}]`);
if (!updated) {
updated = document.createElement("link");
updated.rel = "stylesheet";
updated.type = "text/css";
updated.dataset.linkId = id;
link.parentElement.insertBefore(updated, link);
// At this point we disable the original stylesheet,
// so it won't be applied to the document anymore.
link.disabled = true;
}
// We set the new <link> href...
updated.href = url;
// ...Waiting that is loaded...
await load(updated);
// ...and finally tell to the browser that we don't need
// the blob's URL anymore, so it can be released.
URL.revokeObjectURL(url);
}
<link rel="preload" href="path/to/mystylesheet.css" as="style" onload="this.rel='stylesheet'">
rel="reload"
,但示例中却是 rel="preload"
。 - bb216b3acfd8f72cbc8f899d4d6963javascript:void(function()%7Bvar%20i,a,s;a=document.getElementsByTagName('link');for(i=0;i%3Ca.length;i++)%7Bs=a[i];if(s.rel.toLowerCase().indexOf('stylesheet')%3E=0&&s.href)%20%7Bvar%20h=s.href.replace(/(&%7C%5C?)forceReload=%5Cd%20/,'');s.href=h%20(h.indexOf('?')%3E=0?'&':'?')%20'forceReload='%20(new%20Date().valueOf())%7D%7D%7D)();