LoginSignup
9
8

More than 5 years have passed since last update.

ブラウザ間のメソッドやプロパティの違いを三項演算子を使って吸収する

Posted at

leafletのサンプルコードを読んでて知った方法。

ブラウザによってinnerTextだったりtextContentだったりを使い分けないといけない時に三項演算子を使ってプロパティやメソッドの違いを吸収する

var textarea = document.querySelector("textarea");
textarea["innerText" in textarea ? "innerText" : "textContent" ] = "text";

input要素のoninputとonchangeの違いなんかもこれで行けるらしい。

var input = document.querySelector("input");
input['oninput' in input ? 'oninput' : 'onchange'] = function(){
    console.log(this.value);
};
9
8
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
9
8