移动网站开发中常用的10段JavaScript代码

开发 前端
移动网站开发中常用的10段JavaScript代码,赶快收藏吧!

1、如果网页是在iPhone或Android浏览器中查看,则在主体元素中添加“iPhone”或“Android” 类名

if (navigator.userAgent.match(/iPhone/i)) {  
    $('body').addClass('iPhone');  
} else if (navigator.userAgent.match(/Android/i)) {  
        $('body').addClass('Android');  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

iPhone用户浏览示例:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A537a Safari/419.3
Mozilla/5.0 (iPhone; U; XXXXX like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A477d Safari/419.3

Android用户浏览示例:

Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 1.6; en-gb; Dell Streak Build/Donut AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/ 525.20.1
Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17
Mozilla/5.0 (Linux; U; Android 2.2; en-us; DROID2 GLOBAL Build/S273) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; E10i Build/2.0.2.A.0.24) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

2、移除浏览器地址栏

window.scrollTo(0, 1); 
  • 1.

3、防止网页触摸滚动

notouchmove = function(event) {  
    event.preventDefault();  
}  
<div data-role="page" id="home" ontouchmove="notouchmove(event);">  
...  
</div> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

4、当横向浏览时显示信息

var updateorientation = function (){  
    var classname = '',  
    top = 100;  
    switch(window.orientation){  
        case 0:  
        classname += "normal";  
        break;  
 
        case -90:  
        classname += "landscape";  
        break;  
 
        case 90:  
        classname += "landscape";  
        break;  
 
    }  
 
    if (classname == 'landscape') {  
        if ($('#overlay').length === 0) {  
            window.scrollTo(0, 1);  
            $('body').append('<div id="overlay" style="width: 100%; height:' + $(document).height() + 'px"><span style="top: ' + top + 'px">Landscape view is not supported for this page.</span></div>');  
        }  
    } else {  
        $('#overlay').remove();  
    }  
};  
Usage:  
 
var supportsOrientationChange = "onorientationchange" in window,  
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";  
 
window.addEventListener(orientationEvent, function() {  
    updateorientation();  
}, false); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

5、显示部分描述信息,当点击时显示完整信息

var truncatedesc = function(trunc, len) {  
    if (trunc) {  
      var org = trunc;  
 
      if (trunc.length > len) {  
        trunc = trunc.substring(0, len);  
        trunc = trunc.replace(/w+$/, '');  
 
        trunc = '<span class="truncated">' + trunc;  
        trunc += '<strong class="more-description">...</strong></span>';  
        trunc += '<span class="original" style="display: none;">' + org + '</span>';  
      }  
 
      $('.truncated').live("touchstart touchend"function() {  
        $(this).closest('div').find('.original').show();  
        $(this).closest('div').find('.truncated').hide();  
        return false;  
      });  
 
      return trunc;  
    }  
};  
 
Usage:  
 
truncatedesc(item.description, 100); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

6、收到成功的Ajax请求时,重定向到另一个页面(jQuery mobile)

var ajaxurl = ‘http://…’; // Your web service URL  
 
$.ajax({  
    url: ajaxurl,  
    type: 'GET',  
    processData: false,  
    contentType: "application/json",  
    dataType: "jsonp",  
    success: function(data) {  
        $.mobile.changePage("results.html");  
    },  
    error: function() {  
        alert('Error!');  
    }  
}); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

7、从列表视图的链接中删除活动状态(jQuery mobile)

$('div').live('pageshow'function (event, ui) {  
    $('[data-role=listview] li').removeClass("ui-btn-active");  
}); 
  • 1.
  • 2.
  • 3.

8、从下拉选择中禁用默认的jQuery mobile样式(jQuery mobile)

$(document).bind("mobileinit"function(){  
     $.mobile.page.prototype.options.keepNative = "select";  
}); 
  • 1.
  • 2.
  • 3.

9、动态更新列表视图(jQuery mobile)

var output  = '<li><img src="' + item.image + '" alt="' + item.title + '" />';  
output += '<h3><a href="' + item.url + '">' + item.title + '</a></h3>';  
output += '</li>';     
 
$('#mylistul').append(output).listview('refresh');  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

10、动态添加表单输入和应用默认样式(jQuery mobile)

var html = '<input type="search" name="suburb" id="suburb" placeholder="Enter suburb" />';  
$('#searchform').append(html);  
$('#suburb').textinput(); 
  • 1.
  • 2.
  • 3.

原文:http://qing.weibo.com/1609119537/5fe93731330004ep.html

【编辑推荐】

  1. Google推出JavaScript测试工具Google JS Test
  2. 浅谈JavaScript编程语言的编码规范
  3. 9月Web技术最前沿:jQuery成版本帝
  4. 如今的开发者应了解哪些过去闻所未闻的新技能
  5. 当jQuery遭遇CoffeeScript——妙不可言
责任编辑:陈贻新 来源: 2gua
相关推荐

2011-05-12 17:26:40

移动网站CSS

2011-05-12 17:13:06

移动网站标记语言

2012-02-16 10:39:12

Android版Chr移动网站开发者

2012-03-06 16:57:40

jQuery mobijQuery mobiframework

2013-08-20 14:13:01

网站开发编程

2009-07-01 16:54:20

JSP网站开发教程

2011-07-07 10:10:02

WEB

2014-05-04 11:06:41

移动网站移动设计

2009-05-18 16:59:42

代码PHP编码

2011-05-18 13:47:32

2013-08-29 10:50:48

移动网站性能优化移动web

2019-06-21 10:13:26

JavaScript错误开发

2023-07-04 07:31:12

JavaScriptWeb

2011-03-22 10:03:24

web网站开发

2013-08-27 13:13:29

移动网站性能优化移动web

2019-07-02 10:36:30

JavaScript硬件开发

2020-08-04 06:32:21

JavaScript代码开发

2009-11-27 11:08:11

PHP动态网站开发

2009-06-01 11:16:48

PHP网站开发变量作用域

2023-04-09 14:48:03

JavaScript脚本语言开发
点赞
收藏

51CTO技术栈公众号