使用Beautify.js来美化你的jQuery代码
2011-11-15 17:29
453 查看
日期:2011/11/15 来源:GBin1.com
使用程序或者某些工具自动生成的Javascript格式有时候可能会非常糟糕,这个时候如果我们拥有一个可以自动帮助你美化代码的工具将会非常的给力!今天我们介绍使用Beautify.js来帮助你自动规整jQuery代码。
在线工具地址:http://jsbeautifier.org/
在线下载地址:https://github.com/einars/js-beautify
在线演示
Javascript代码:
以上代码查询DOC中的class为beautify的节点,然后调用beautify。
HTML
修改了的beautify(),然后保存为gbbeautify.js,如下:
原文来自:使用Beautify.js来美化你的jQuery代码
使用程序或者某些工具自动生成的Javascript格式有时候可能会非常糟糕,这个时候如果我们拥有一个可以自动帮助你美化代码的工具将会非常的给力!今天我们介绍使用Beautify.js来帮助你自动规整jQuery代码。
在线工具地址:http://jsbeautifier.org/
在线下载地址:https://github.com/einars/js-beautify
在线演示
Javascript代码:
$(document).ready(function() { $('.beautify').each(function() { console.log(this); beautify(this); }); });
以上代码查询DOC中的class为beautify的节点,然后调用beautify。
HTML
<B> CSS Code </B> <pre>body{color:#fff;font-size:12px}</pre> <pre class="beautify">body{color:#fff;font-size:12px}</pre> <B> jQuery Code </B> <pre>$('#gbin1').html('Just a test for beautify.js, enjoy!').animate({fontSize: "15px"}, 500);</pre> <pre class="beautify">$('#gbin1').html('Just a test for beautify.js, enjoy!').animate({fontSize: "15px"}, 500);</pre>
修改了的beautify(),然后保存为gbbeautify.js,如下:
var the = { beautify_in_progress: false }; // this dummy function alleviates Chrome large string corruption by probably shoveling the corruption bug to some other area if (/chrome/.test(navigator.userAgent.toLowerCase())) { String.prototype.old_charAt = String.prototype.charAt; String.prototype.charAt = function (n) { return this.old_charAt(n); } } function unpacker_filter(source) { var trailing_comments = ''; var comment = ''; var found = false; do { found = false; if (/^\s*\/\*/.test(source)) { found = true; comment = source.substr(0, source.indexOf('*/') + 2); source = source.substr(comment.length).replace(/^\s+/, ''); trailing_comments += comment + "\n"; } else if (/^\s*\/\//.test(source)) { found = true; comment = source.match(/^\s*\/\/.*/)[0]; source = source.substr(comment.length).replace(/^\s+/, ''); trailing_comments += comment + "\n"; } } while (found); return trailing_comments + source; } function beautify(elem) { if (the.beautify_in_progress) return; the.beautify_in_progress = true; // var source = $('#source').val(); var source = $(elem).html(); var indent_size = $('#tabsize').val(); var indent_char = indent_size == 1 ? '\t' : ' '; var preserve_newlines = $('#preserve-newlines').attr('checked'); var keep_array_indentation = $('#keep-array-indentation').attr('checked'); var brace_style = $('#brace-style').val(); if ($('#detect-packers').attr('checked')) { source = unpacker_filter(source); } var comment_mark = '<-' + '-'; var opts = { indent_size: indent_size, indent_char: indent_char, preserve_newlines:preserve_newlines, brace_style: brace_style, keep_array_indentation:keep_array_indentation, space_after_anon_function:true}; if (source && source[0] === '<' && source.substring(0, 4) !== comment_mark) { $(elem).html( style_html(source, opts) ); } else { var v = js_beautify(unpacker_filter(source), opts); $(elem).html(v); } the.beautify_in_progress = false; }
原文来自:使用Beautify.js来美化你的jQuery代码
相关文章推荐
- 使用Beautify.js来美化你的jQuery代码
- 如何解决ecshop中jquery冲突?使用jquery.json.js代替transport.org.js中部份代码,完美解决问题。
- 使用jQuery的时候,js文件代码位置规范
- 使用JS或jQuery模拟鼠标点击a标签事件代码
- 使用jquery.upload.js实现异步上传示例代码
- 使用JS或jQuery模拟鼠标点击a标签事件代码
- 使用js_beautify统一团队代码风格
- 使用JS或jQuery模拟鼠标点击a标签事件代码
- 如何使用jquery动态加载js,css文件实现代码
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- 使用JS或jQuery模拟鼠标点击a标签事件代码
- 使用JS或jQuery模拟鼠标点击a标签事件代码
- 使用JS或jQuery模拟鼠标点击a标签事件代码
- 使用jquery.upload.js实现异步上传示例代码
- jquery关于load()加载页面遇见的坑(js代码使用不了)
- 如何使用jquery动态加载js,css文件实现代码
- 使用jquery与图片美化checkbox和radio控件的代码(打包下载)
- 使用jquery与图片美化checkbox和radio控件的代码(打包下载)
- 本地使用js或jquery操作cookie在谷歌浏览器chrome中不生效
- 通过一段代码简单说js中的this的使用