成都创新互联网站制作重庆分公司

移动网站开发中常用的JavaScript代码有哪些

本篇内容介绍了“移动网站开发中常用的JavaScript代码有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联专业为企业提供青原网站建设、青原做网站、青原网站设计、青原网站制作等企业网站建设、网页设计与制作、青原企业网站模板建站服务,10余年青原做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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

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

2、移除浏览器地址栏

window.scrollTo(0, 1);

3、防止网页触摸滚动

notouchmove = function(event) {      event.preventDefault();  }    ...  

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('Landscape view is not supported for this page.
');          }      } else {          $('#overlay').remove();      }  };  Usage:   var supportsOrientationChange = "onorientationchange" in window,  orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";   window.addEventListener(orientationEvent, function() {      updateorientation();  }, false);

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 = '' + trunc;          trunc += '...';          trunc += '' + org + '';        }         $('.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);

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!');      }  });

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

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

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

$(document).bind("mobileinit", function(){       $.mobile.page.prototype.options.keepNative = "select";  });

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

var output  = '
  • ';  output += '

    ' + item.title + '

    ';  output += '
  • ';      $('#mylistul').append(output).listview('refresh');

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

    var html = '';  $('#searchform').append(html);  $('#suburb').textinput();

    “移动网站开发中常用的JavaScript代码有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


    分享文章:移动网站开发中常用的JavaScript代码有哪些
    文章链接:http://cxhlcq.com/article/ggepce.html

    其他资讯

    在线咨询

    微信咨询

    电话咨询

    028-86922220(工作日)

    18980820575(7×24)

    提交需求

    返回顶部