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

本篇内容介绍了“移动网站开发中常用的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');  }

2、移除浏览器地址栏

window.scrollTo(0, 1);

3、防止网页触摸滚动

notouchmove = function(event) {      event.preventDefault();  }  <div data-role="page" id="home" ontouchmove="notouchmove(event);">  ...  </div>

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

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

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

var ajaxurl = &lsquo;http://&hellip;&rsquo;; // 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  = '<li><img src="' + item.image + '" alt="' + item.title + '" />';  output += '<h4><a href="' + item.url + '">' + item.title + '</a></h4>';  output += '</li>';      $('#mylistul').append(output).listview('refresh');

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

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

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