有没有重新加载css而不重新加载页面的简单方法?

编程入门 行业动态 更新时间:2024-10-26 08:21:21
本文介绍了有没有重新加载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?

推荐答案

在编辑页面上,而不是将正常方式(使用< link> 标记),将其全部写入< style> 标记。编辑 innerHTML 属性将自动更新页面,即使没有到服务器的往返。

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 the innerHTML property of that will automatically update the page, even without a round-trip to the server.

<style type="text/css" id="styles"> p { color: #f0f; } </style> <textarea id="editor"></textarea> <button id="preview">Preview</button>

使用jQuery的Javascript:

The Javascript, using jQuery:

jQuery(function($) { var $ed = $('#editor') , $style = $('#styles') , $button = $('#preview') ; $ed.val($style.html()); $button.click(function() { $style.html($ed.val()); return false; }); });

这应该是!

如果你想要真的想,附加功能到textarea键下,虽然你可以得到一些不必要的副作用(页面将不断变化,你输入)

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)

编辑:测试并正常工作(至少在Firefox 3.5中,虽然应该与其他浏览器兼容)。请参见此处的演示: jsbin/owapi

Edit: tested and works (in Firefox 3.5, at least, though should be fine with other browsers). See demo here: jsbin/owapi

更多推荐

有没有重新加载css而不重新加载页面的简单方法?

本文发布于:2023-10-12 21:02:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   而不   页面   简单   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!