LoginSignup
8
13

More than 5 years have passed since last update.

jQueryを書く際に使っているテンプレート

Last updated at Posted at 2017-01-26

jQueryで実装する時に使ってるテンプレ

js,jQueryを書いてると汚くなる事が多いので、
記述する際は下記テンプレを使って書いてます。

template
var hoge = function(){
    var self;

    this.init = function(){
        self = this;
    }

    this.init();
}
var Hoge = new hoge();

上記だけでは解りずらいので、使用例を記載します。

以下はradioボタン変更に応じてタグの挿入・削除をする処理となります。

使用例
var tag = function(){
    var self;
    var $addpoint = $('#addpoint');//タグを追加する場所
    var tagclass = 'add_sample';//追加するタグのクラス名
    var ID = 1;//タグを追加するradioボタンのID
    var TAG = '<p class="add_sample">追加されたタグ</p>';//タグ

    this.init = function(){
        self = this;

        $('input[name="parts_type"]:radio').change( function() {
            if ($(this).val() == ID) {
                self.add();
            } else {
                self.remove();
            }
        });
    }

    this.add = function(){
        $addpoint.before(TAG);
    }

    this.remove = function(){
        $('.'+ tagclass ).remove();
    }

    this.init();
}
var Tag = new tag();
8
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
13