在“编辑”页面上,不要以常规方式(带有
<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; });});
就是这样!
如果您真的想花哨的话,请将功能附加到textarea的keydown上,尽管您可能会得到一些不良的副作用(键入时页面会不断变化)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)