<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8"/>
        <title></title>
        <script type="text/javascript" src="../js/jquery-1.11.0.js"></script>
        <script type="text/javascript">
            $(function() {
                var $input = $('input'),
                    $button = $('button'),
                    $label = $('label');
                $button.click(function() {
                    var content = $.trim($input.val());
                    if (content) {
                        var arr = [], diff = 3 - (content.length % 3 - 1);

                        for (var i = 0; i < content.length - 1; i++) {
                            arr.push(content.charAt(i));
                            if (!((i + diff) % 3)) {
                                arr.push(',');
                            }
                        }
                        arr.push(content.charAt(content.length - 1));
                        $label.html(arr.join(''));
                    }

                });
            });
        </script>
    </head>
    <body>
        <input type="text" value=""/>
        <button>转换</button>
        <label></label>
    </body>
</html>

 

posted on 2015-03-02 22:33  爱生活的夜  阅读(523)  评论(0编辑  收藏  举报