LoginSignup
21
19

More than 5 years have passed since last update.

よく忘れるJavaScript(&jQuery)まとめ

Last updated at Posted at 2016-03-25

よく忘れるJavaScriptまとめ

■日付関連

現在の日付を取得
date = new Date();

現在の年を取得
date.getFullYear();

現在の月を取得
date.getMonth() + 1;

現在の日を取得
date.getDate();

現在の曜日を取得
date.getDay();

現在の時を取得
date.getHours();

現在の分を取得
date.getMinutes();

現在の秒を取得
date.getSeconds();

■3桁ごとコンマ入り

var num = 1000;
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

■URLに含まれた文字列検索

if (window.location.href.indexOf("index") > 0) {
}

■対象以外を押した時DIVを消す

$(document).on("click", function(e) {
  if ($(e.target).parents("DIVを指定").size() < 1) {
    $("DIVを指定").hide();
  }
});

■文字列制限関連

$("document").on("keyup", "テキストフィールド", function(e) {
  var len = $(this).val().length; //入力された文字数
  max = 50; //制限文字数
  var str = $(this).val().substr(0, max); //入力された文字(50まで)
  if (len > max) { //制限より長いなら(オーバー)
    $(this).val(str); //50までの文字列(str)を入れる
    return false;
  }
});

■onクラスが入ったliを探す

<ul>
  <li>1</li>
  <li class="on">2</li>
  <li>3</li>
</ul>

var idx = $("ul").find(".on").index("ul li");
if (idx > -1) {
  console.log($("ul li").eq(idx).html());
}

result : 2
21
19
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
21
19