2

如果我在發生方向更改時觸發onChange函數,如何在onChange中設置一個值來更新jquery選擇器。例如:評估orientationEvent並設置一個值?

$(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 
    }); 

回答

8

您需要將更新放到onChanged函數中的圖像中,這樣每次方向更改時,圖像HTML都會更改。

$(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); 

});