`
jjfat
  • 浏览: 289370 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用beautify.js来美化你的jQuery代码

阅读更多

日期:2011/11/15  来源:GBin1.com

使用程序或者某些工具自动生成的Javascript格式有时候可能会非常糟糕,这个时候如果我们拥有一个可以自动帮助你美化代码的工具将会非常的给力!今天我们介绍使用Beautify.js来帮助你自动规整jQuery代码。

在线演示

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代码

分享到:
评论

相关推荐

    beautifyhtml.js

    使用程序或者某些工具自动生成的Javascript格式有时候可能会非常糟糕,这个时候如果我们拥有一个可以自动帮助你美化代码的工具将会非常的给力!今天我们介绍使用Beautify.js来帮助你自动规整jQuery代码。

    beautify-jquery:jQuery 包装器到 JS Beautfier

    jQuery.Beautify 是的包装器 安装 待定 例子 options 参数中的所有内容都将传输到 JS Beautifier $ ( expression ) . beatify ( { indent_size : 2 } ) ; $ ( 'pre code' ) . beautify ( options ) ; // Options ...

    jQuery美化select下拉框

    在提供的压缩包文件“beautify select box”中,可能包含了示例代码、CSS样式和jQuery插件文件,你可以根据这些资源进行实践和学习,进一步理解并掌握jQuery美化select下拉框的技术。在实际项目中,记得根据具体需求...

    vscode_jquery安装包.rar

    6. **代码格式化与美化**:VSCode还可以通过安装如"Beautify"或"Prettier"等插件,自动格式化和美化JavaScript代码,使代码更易于阅读和维护。 7. **代码片段增强**:jQuery插件提供的代码片段可以加速开发过程,...

    css&js格式化工具

    最后,`beautify-tests.js`很可能是一些测试用例,用于验证代码美化功能是否正常工作。测试是软件开发过程中的关键环节,确保格式化工具在各种情况下都能正确地格式化代码。 总的来说,这些工具和文件共同构成了一...

    优美代码力作

    使用代码美化工具,如Beautify、SublimeLinter和Grunt,可以自动检查和格式化代码,确保代码的整洁度和一致性。 遵循【惯用法】是保持代码风格一致的重要手段。例如,注释符号后面应留空格,变量应先声明后使用,...

    VSCode开发工具以及离线插件.zip

    10. **beautify**: `HookyQR.beautify-1.5.0.vsix`是用于美化HTML、CSS和JavaScript代码的插件,它能按照用户配置的规则对代码进行格式化。 11. **auto-rename-tag**: `formulahendry.auto-rename-tag-0.0.15.vsix`...

    javascript排版工具

    JavaScript排版工具,如文件名所示“einars-js-beautify-c3b5a57”,是用于美化和格式化JavaScript代码的实用程序。在编程过程中,代码的可读性和整洁性至关重要,尤其是在团队协作和代码审查时。JavaScript排版工具...

    select标记美化--JS式插件、后期加载

    JavaScript美化时,依赖于jQuery库。通过jQuery的$(document).ready()函数确保DOM完全加载后执行美化脚本。利用选择器找出所有带有特定class(如"beautify")的元素进行美化处理。 #### 3. 创建与原同步的元素 为了...

    select标记美化–JS式插件、后期加载

    &lt;select&gt;标签的外观问题很恼人,各个浏览器都不一致,单单就IE,一个版本就一个长相,...标记,我们用一个class名称 “beautify” 来确定,没有这个样式的&lt;select&gt;则将被忽略 var selects = $(“select.b

Global site tag (gtag.js) - Google Analytics