将动态 CSS 代码注入文档以便可以在运行时预览所做的更改的最佳方法是什么?
我在 HTML 页面中有一个 TextArea,我将在其中键入 CSS 代码。我想从文本区域更新页面的样式。这是我目前使用的方法。
<!DOCTYPE html>
<html>
<head>
<title>Dynamic CSS Experiments</title>
<meta charset=utf-8 />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<style id="dynamic-css" type="text/css"></style>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
jQuery(function ($) {
var styleTag, textArea;
styleTag = $("#dynamic-css");
textArea = $("#css-ta");
textArea.on('keydown', function () {
styleTag.html(textArea.val())
});
});
</script>
</head>
<body>
<textarea name="css" id="css-ta" cols="30" rows="10"></textarea>
...
</body>
</html>
这是最好的方法吗?还有其他聪明的方法吗?