LoginSignup
5
1

More than 5 years have passed since last update.

【超シンプル】PCとモバイルのjquery切り替え

Last updated at Posted at 2018-04-25
<script>
  jQuery(function () {
    var ua = navigator.userAgent;
    if (ua.indexOf('iPhone') > 0 || ua.indexOf('Android') > 0 && ua.indexOf('Mobile') > 0) {
      // スマートフォン用コード
      jQuery('#output').html('<img src="/images/hoge.jpg">');
    } else if (ua.indexOf('iPad') > 0 || ua.indexOf('Android') > 0) {
      // タブレット用コード
    } else {
      // PC用コード
    }
  });
</script>

<div id="output"><div>

コンフリクトに疲れたので $ ではなく jQuery を使用。ちゃんと動いたら $ に置換。
出力後のhtmlは以下。

html(モバイル)
<div id="output"><img src="/images/hoge.jpg"></div>
5
1
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
5
1