根据ipad上的方向更改更新值

时间:2012-01-05 00:56:01

标签: javascript jquery ipad mobile mobile-safari

根据方向更改更新值的最佳方法是什么?

示例加价值。

<span class="week">Thursday</span>

所以如果它是风景,它会说星期四,如果是肖像,它会说 Thu

我可以使用subString(0,3)更新字符串,但我不知道如何在之后更改它。

继承了我所获得的方向改变功能:

/* orientation change */
function orient() {  
    if (window.orientation == 0 || window.orientation == 180) {
        $('body').attr('class', 'portrait');
        orientation = 'portrait';
        return false;
    }
    else if (window.orientation == 90 || window.orientation == -90) {
        $('body').attr('class', 'landscape');
        orientation = 'landscape';
        return false;
    }
}
/* all orientation function on page load */
$(function(){
    orient();
});
/* call orientation function on orientation change */
$(window).bind( 'orientationchange', function(e){
    orient();
});

2 个答案:

答案 0 :(得分:1)

您可以使用数据属性存储日期的长短版本:

<span class="week" data-long="Thursday" data-short="Thu">Thursday</span>

之后,只需稍加改动即可使用它:

function orient() {  
    orientation = window.orientation == 0 || window.orientation == 180 ? 'portrait' : 'landscape';
    $('body').attr('class', orientation );
    $('.week').html(function() {
        var $this = $(this);
        return $this.data(orientation == 'landscape' ? 'long' : 'short');
    });
    return false;
}

答案 1 :(得分:0)

您可以像这样设置文字:

$(".week").html("Thu"); 

$(".week").html("Thursday"); 

如果您需要保存原始长版本,则可以使用.data()方法首次保存,以便日后访问。

var week = $(".week");
if (!week.data("day")) {
    week.data("day", week.html());
}
var day = week.data("day");
if (orientation == "landscape") {
    week.html(day);
} else {
    week.html(day.substr(0,3));
}