2

向きの変更が発生したときにonChange関数を起動する場合、jqueryセレクターを更新するonChange内の値を設定するにはどうすればよいですか。例えば:

  $(document).ready(function(){    
    var onChanged = function() {
            if(window.orientation == 90 || window.orientation == -90){
                image = '<img src="images/land_100.png">';
            }else{
                image = '<img src="images/port_100.png">';
            }
     }
        $(window).bind(orientationEvent, onChanged).bind('load', onChanged);
        $('#bgImage').html(image); //won't update image
   });
4

1 に答える 1

8

向きが変わるたびに画像の HTML が変更されるように、onChanged 関数内に画像の更新を配置する必要があります。

$(document).ready(function(){   

   // The event for orientation change
   var onChanged = function() {

      // The orientation
      var orientation = window.orientation,

      // If landscape, then use "land" otherwise use "port"
      image = orientation == 90 || orientation == -90 ? "land" : "port";

      // Insert the image
      $('#bgImage').html('<img src="images/'+image+'_100.png">');

   };

   // Bind the orientation change event and bind onLoad
   $(window).bind(orientationEvent, onChanged).bind('load', onChanged);

});
于 2011-03-04T16:11:39.193 に答える