Home > Web Front-end > JS Tutorial > body text

What functions does Mobile use to operate H5?

php中世界最好的语言
Release: 2018-04-26 11:40:48
Original
1641 people have browsed it

This time I will bring you what functions are available for Mobile operation of H5, and what are the precautions for Mobile operation of H5. The following is a practical case, let's take a look.

1. $.mobile.changePage() & $.mobile.loadPage()1.$.mobile.changePage()

$.mobile The .changePage() method will be automatically called when the page is loaded. If this page is a different "page" in the current document, it will go to this new page to hide the old page; if this page is an external page, that is, the page is not the same as the current page. In the same document (essentially, the new page is not in the current DOM), $.mobile.changePage() will first call $.mobile.loadPage() to insert the elements of the external page into the DOM, and then display the new page. This is also a simple retelling of the page loading process.

$.mobile.changePage() has two parameters to (string or object, required) and options (object, optional), as follows:

(1)——to ( string or object, required)

to is a required parameter, its value can be string (string, such as "about/us.html") or object (object, such as $("#about")), This is mainly for two different pages, the string form is an external page link, and the object is a different "page" in the same document, such as "#page2", $.mobile.changePage() will process it to include this The jQuery object of the DOM is in the form of $('#page2'), and $.mobile.changePage() will internally determine the form of the to parameter. If it is a string, $.mobile.loadPage() will be called to insert the elements of the external page. Go to the DOM and then display the page.

(2)—— options (object, optional)

options are optional parameters, and their value is object. This object contains multiple attributes, and these attributes store a page. Various parameters, jQuery Mobile will control how to load the page and initialize the page based on these parameters. The specific attribute values ​​are as follows:

allowSamePageTransition (boolean, default value: false) By default, $.mobile.changePage() ignores requests to jump to the current page. Set this property to "true" to allow such requests

changeHash (boolean, default: true) Determine whether the hash value on the address bar should be updated.

data (object or string, default value: undefined) Data sent during Ajax request. Only available if the value of the to parameter is a URL.

dataUrl (string, default: undefined) The URL to be updated in the browser's address bar when the browser completes the page transition. If not specified otherwise, the attribute value of data-url is used.

pageContainer (jQuery collection, default: $.mobile.pageContainer) Specifies the jQuery object that contains the page's DOM object.

reloadPage (boolean, default: false) Forces a page refresh even if the DOM of the page container is ready. Only available if the value of the to parameter is a URL.

reverse (boolean, default value: false) Set the direction of the page transition animation. Page transitions are reversed when this property is set to "true".

role (string, default value: undefined) Use the data-role value when displaying the page. The default is undefined, which depends on the value of the element's @data-role attribute (the value of data-role on the tag).

showLoadMsg (boolean, default value: true) Sets whether to display the loading prompt message when loading an external page.

transition (string, default value: $.mobile.defaultPageTransition) Sets the transition animation used when the page is loaded.

type (string, default: "get") Sets the method ("get" or "post") used when requesting the page. Only available if the value of the to parameter is a URL.

Here is an example to illustrate the use of $.mobile.changePage(). Manually calling this method can trigger a jump to a new page. For example, when an error occurs in Web Apps, you can jump to an error prompt. page.

// 转入到 "about us" 页面并使用 "slideup" 转场动画
$.mobile.changePage( "about/us.html", { transition: "slideup"} );
// 转入到 "search results" 页面, 使用 来自 id 为 search 的表单数据,并把页面请求方式改为 "post"
$.mobile.changePage('searchresults.php', {
  type: "post",
  data: $("form#search").serialize()
});
// 转入到 "confirm" 页面并使用 "pop" 转场动画,不更新历史记录(地址栏哈希值不更新)
$.mobile.changePage('../alerts/confirm.html', {
  transition: "pop",
  reverse: false,
  changeHash: false
});
Copy after login
When an error occurs, you can jump to an error page.

$(function(){
  // 发生错误
  // 已 "slideup" 的方式弹出对话框提示发生了错误
  $.mobile.changePage('../alerts/confirm.html', {
    transition: "slideup",
    role: "dialog"
  });
});
Copy after login

2.$.mobile.loadPage()

如上面所述,$.mobile.loadPage() 用于加载一个外部页面,但这里的加载是指把外部页面的元素插入到当前 DOM 中,并在插入之前对页面内容进行 jQuery Mobile 增强 。该方法会被 $.mobile.changePage() 调用,条件是 $.mobile.loadPage() 的第一个参数 to 为 string (即加载的页面为外部页面)。这个方法仅仅是进行插入 DOM 操作,不会影响当前激活的页面,所以它可以用于在后台加载页面(只插入 DOM 而不显示页面),该方法会返回一个 deferred 对象,包含页面请求成功与否等相关信息,并会在页面增强和插入 DOM 后分解这个对象。

$.mobile.loadPage() 有两个参数 url (string or object, 必须) 和 options (object, 可选),具体情况如下:

(1)—— url (string or object, 必须)

该参数的值可以为 string 或 object ,无论是何种形式,必须为一个绝对或相对 URL 。若是由 $.mobile.changePage() 调用本方法时,则该参数值为 $.mobile.changePage() 的 to 参数值。

(2)—— options (object, 可选)

options 为可选参数,其值为 object (对象),实际上如由 $.mobile.changePage() 调用本方法时,该参数值为 $.mobile.changePage() 的 options 参数值。因此这个 object 与 $.mobile.changePage() 的 options 选项值相同,其具体属性值请参考 $.mobile.changePage() 中 options 的属性值。

这里引用例子说明一下 $.mobile.loadPage() 的使用方法,手动调用该方法可以在后台加载外部页面的元素并且不影响当前激活页面。
    

// 把 "about us" 页面加载到 DOM
$.mobile.loadPage('about/us.html');
 
// 转入到 "search results" 页面, 使用来自 id 为 search 的表单数据,并把页面请求方式改为 "post"
$.mobile.loadPage('searchresults.php', {
  type: "post",
  data: $('form#search').serialize()
});
Copy after login

二. $.fn.jqmData() & $.fn.jqmRemoveData()当页面中使用了 jQuery Mobile ,jQuery Mobile 会使用 jqmData 和 jqmRemoveData 代替 jQuery 核心的 data 和 removeData 方法(注意,这包括了 jQuery 中的 $.fn.data , $.fn.removeData , $.data , $.removeData 和 $.hasData),这两个方法会自动获取和设置 data 属性的命名空间(即使当前没有使用命名空间)。

这两个方法的参数对应着 jQuery 的 data 和 removeData 方法的参数,具体如下:

(1)—— jqmData( jQuery.data() )

element 与该 data 属性相关联的 DOM 对象
key data 的命名字符串
value data 属性的值
(2)—— jqmRemoveData ( jQuery.removeData() )

element 与需要移除 data 属性相关联的 DOM 对象
name 需要移除的 data 的命名字符串
注意:当通过 jQuery Mobile data 属性寻找元素时,请使用自定义选择器:jqmData() ,它会在查询元素时自动合并 data 属性的命名空间。例如,你应该使用 $('p:jqmData(role="page")') 代替 $('p[data-role="page"]') ,因为前者会自动映射到 $("p[data-"+ $.mobile.ns +'role="page"]') ,而不需要强制把选择器与命名空间连接起来。例如:命名空间为 rn ,若使用后者即为 $('p[data-rn-role="page"]') ,这时如果更改了命名空间,则这个选择器便会失效,而使用前者则会自动映射到当前选择器中,即使更改命名空间也不会造成这个选择器失效。

但是也有例外的情况,就是根据 URL 值在命名空间中选择 data 属性,例如,jQuery Mobile 可以使用 :jqmData(url) 跟踪一个页面来自哪里,根据 URL 中的命名空间来选择空间内的 data 属性。这要求需要在选择器的括号内填写一个有效的 URL 。

三. $.fn.jqmEnhanceable()这是判断元素是否要进行 jQuery Mobile 增强的方法,默认情况下,所有 jQuery Mobile 组件都会通过此方法放入 jQuery Mobile 增强集,以交给另一函数进行 jQuery Mobile 增强,包括添加相应的 HTML , CSS class 和交互。这是 jQuery Mobile 默认调用的方法,并且没有任何可选参数,但该方法仍有一个很值得注意的地方,在方法的实现函数内部,会判断默认配置 $.mobile.ignoreContentEnabled 的值,若为 true ,则会对调用该方法的 jQuery 对象的 DOM 父节点遍历,若父节点标签上设置了 data-enhance = "false" ,则不让该 DOM 对象加入增强集。事实上 jQuery Mobile 的官方文档并没有阐述 $.fn.jqmEnhanceable() 的具体使用方法,反而用了不少篇幅介绍这个注意事项。

另外还需要注意的是,遍历操作会设计元素的所有父节点,因此即使遍历一个很小的 jQuery 对象集的父节点也会消耗很多资源,开发则需要谨慎使用。若开发时已经明确不需要对某元素进行增强,建议还是直接不使用 data-role 触发相应的组件。

而如何设置 $.mobile.ignoreContentEnabled 的值,可以参考《使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础》

四. $.fn.jqmHijackable()这是判断元素是否加入 jQuery Mobile Ajax 导航,即使用 Ajax 进行处理的方法,与 $.fn.jqmEnhanceable() 相似,默认情况下这也是会为所有链接和表单提交执行,使到它们可以加入 jQuery Mobile Ajax 处理集合,交给另一函数处理。在 jQuery Mobile 内部,本方法与 $.fn.jqmEnhanceable() 最终都是调用了 haveParents 方法,来判断元素是否应该加入相应的集合。因此,本方法执行时会判断默认配置 $.mobile.ignoreContentEnabled ,若为 true ,则会对调用该方法的 jQuery 对象的 DOM 父节点遍历,若父节点上标签上设置了 data-ajax = "false" ,则不让该 DOM 对象加入 Ajax 导航集。当然,在使用这个特性时也需要注意遍历带来的大量资源消耗。

五. $.mobile.loading()该方法从 jQuery Mobile 1.2 开始正式引进,控制显示或隐藏页面加载信息,包含两个参数,第一个是控制页面信息加载与否,只有 "show" 和 "hide" 两个值,第二个参数为多属性对象,具体的属性如下:

theme (string, 默认值: "a") 加载信息条的主题样式
text (string, 默认值: "loading") 加载信息条的文字内容
textonly (boolean, 默认值: false) 若设置为 true ,则加载页面时 "spinner" 图片(即旋形加载提示图,1.0 及之前版本为条形加载图)会被隐藏。
textVisible (boolean, 默认值: false) 若设置为 true ,提示的文字内容会置于 spinner 之下
html (string, 默认值: "") 如果个属性值不为空,则这个值会替代整个加载信息条的 HTML
下面引用例子说明 $.mobile.loading 的使用方法。

// 提示页面正在加载
$.mobile.loading('show');
 
// 加载信息条使用主题样式 "b" , 自定义提示文字内容,隐藏 "spinner" 图片
$.mobile.loading('show', { theme: 'b', text: '给力的加载中', textonly: true });
Copy after login

以下两个方法在 jQuery Mobile 1.2 中不赞成使用

六. $.mobile.hidePageLoadingMsg()显示页面加载信息,基于 $.mobile.loadingMessage 配置出来,具体有三个参数

theme (string, default: "a") The theme swatch for the message.
msgText (string, default: "loading") The text of the message.
textonly (boolean, default: false) If true, the "spinner" image will be hidden when the message is shown.
例子:

// 使用主题样式 "b" ,自定义提示文字内容,隐藏 "spinner" 图片
$.mobile.showPageLoadingMsg('b', 'This is only a test', true);
Copy after login

在 jQuery Mobile 1.2 中,建议使用 $.mobile.loading('show') 代替。

七. $.mobile.hidePageLoadingMsg()隐藏页面加载信息,基于 $.mobile.loadingMessage 配置出来,没有参数。

例子:

// 隐藏页面加载提示信息
$.mobile.hidePageLoadingMsg();
Copy after login

在 jQuery Mobile 1.2 中,建议使用 $.mobile.loading('hide') 代替。

八. $.mobile.fixedToolbars.show()固定工具栏(包括固定的头部栏和尾部栏)可以通过点击屏幕在显示与隐藏之间切换,而这个方法则是手动显示一次工具栏。

它具有一个参数 immediately(boolean, 可选) 。把它设置为 true ,当前激活页面的所有固定工具栏都会立即显示出来。若设置为 false 或者没有指定,则会通过 100ms 的渐变显示出来。注意如 document 的 resize 和 scroll 等事件会导致额外的延时显示。

例子:

// 显示固定工具栏并且显示默认的渐变动画
$.mobile.fixedToolbars.show();
// 立即显示固定工具栏
$.mobile.fixedToolbars.show(true);
Copy after login

在 jQuery Mobile 1.1 中,不建议使用该方法,jQuery Mobile 并没有明确原因,但根据 jQuery Mobile 的习惯,这很可能是因为这个方法有很多不稳定性,如上面提到的遇到某些方法会出现额外的延时显示,这对于建立 Web Apps 是很不好的影响因素,它会从底层部分为 Web Apps 带来不可预知的问题。

九. $.mobile.fixedToolbars.hide()固定工具栏(包括固定的头部栏和尾部栏)可以通过点击屏幕在显示与隐藏之间切换,而这个方法则是手动隐藏一次工具栏。

与 $.mobile.fixedToolbars.show() 相似,它具有一个参数 immediately(boolean, 可选) 。把它设置为 true ,当前激活页面的所有固定的工具栏都会立即隐藏。若设置为 false 或者没有指定,则会通过 100ms 的渐变隐藏起来。注意如 document 的 resize 和 scroll 等事件也会增加隐藏动画的时间。

例子:

// 隐藏固定工具栏并显示默认的渐变动画
$.mobile.fixedToolbars.hide();
// 立即隐藏固定工具栏
$.mobile.fixedToolbars.hide(true);
Copy after login

在 jQuery Mobile 1.1 中,也不建议使用该方法。

十. $.mobile.path.parseUrl()本方法用于解析一个 URL 和它的相对形式,并且它的相关成分放入一个对象中,方便访问这些 URL 相关成分。当解析相对形式的 URL 时,返回的对象会自动为绝对形式 URL 中没有的成分(如 protocol, host , etc )添加空字符串作为值。另外,当解析的 URL 没有 authority (见下面的列表),返回的对象中的 pathname 属性会包含通信协议冒号后的数据。

$.mobile.path.parseUrl() 只有一个参数 URL (string, 可选) ,其值为一个 URL 的相对或绝对形式。

另外,$.mobile.path.parseUrl() 会返回一个包含 URL 相关成分的对象,这个对象的属性是模仿浏览器中的 location 对象。具体的属性如下:

hash URL 中的一个部分,从 URL 中第一个 "#" 开始的部分。
host URL 的主机名及端口。
hostname URL 的主机名。
href 被解析的原始 URL 。
pathname URL 所引用的文件或目录的路径。
port URL 中指定的端口。大多数 URLs 依赖于数据传输协议所用的默认端口,所以这个值在大多数时候都可能是空字符串。
protocol 数据传输协议,URL 中 ':' 之前的部分。
search URL 中的从 "?" 字符开始的部分,代表 URL 查询。另外它也包括提供给入口的额外成分,如一些常见形式的开发者访问。
authority URL 的用户名,密码,主机名
directory pathname 中的目录部分,并且不包括任何文件名。
domain URL 中的数据传输协议和用户名,密码,主机名等信息,即域。
filename pathname 中的文件部分,并且不包括任何目录名。
hrefNoHash 从原始 URL 中减去 hash 部分。
hrefNoSearch 从原始 URL 中减去 hash 和 search 部分。
password authority 中的 password 部分。
username authority 中的 username 部分。
例子:

// 解析一个 URL
var obj = $.mobile.path.parseUrl("http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234");
// 解析这个 URL 会返回一个包含以下属性的对象
// obj.href:     http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content
// obj.hrefNoHash:  http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread
// obj.hrefNoSearch: http://jblas:password@mycompany.com:8080/mail/inbox
// obj.domain:    http://jblas:password@mycompany.com:8080
// obj.protocol:   http:
// obj.authority:  jblas:password@mycompany.com:8080
// obj.username:   jblas
// obj.password:   password
// obj.host:     mycompany.com:8080
// obj.hostname:   mycompany.com
// obj.port:     8080
// obj.pathname:   /mail/inbox
// obj.directory:  /mail/
// obj.filename:   inbox
// obj.search:    ?msg=1234&type=unread
// obj.hash:     #msg-content
Copy after login

十一. $.mobile.path.makePathAbsolute()把一个相对的文件或目录路径转化为绝对路径。

具有两个参数 relPath (string, 必须) 和 absPath (string, 必须) ,具体如下:

(1)—— relPath (string, 必须)

其值为一个相对的文件或目录路径。

(2)—— absPath (string, 必须)

用于解析的一个绝对的文件或相对的路径。

$.mobile.path.makePathAbsolute() 会返回一个包含相对路径的绝对路径版本的字符串。

例子:

// 返回: /a/b/c/file.html
var absPath = $.mobile.path.makePathAbsolute("file.html", "/a/b/c/bar.html");
 
// 返回: /a/foo/file.html
var absPath = $.mobile.path.makePathAbsolute("../../foo/file.html", "/a/b/c/bar.html");
Copy after login

十二. $.mobile.path.makeUrlAbsolute()把一个相对 URL 转化为绝对 URL 。

具有两个参数 relUrl (string, 必选) 和 absUrl (string, 必选) ,具体如下:

—— relUrl (string, 必选)

一个相对形式的 URL 。

—— absUrl (string, 必选)

用于解析的一个绝对的文件或相对的路径。

$.mobile.path.makeUrlAbsolute() 会返回一个包含相对 URL 的绝对 URL 版本的字符串。

例子:

// 返回: http://foo.com/a/b/c/file.html
var absUrl = $.mobile.path.makeUrlAbsolute("file.html", "http://foo.com/a/b/c/test.html");
 
// 返回: http://foo.com/a/foo/file.html
var absUrl = $.mobile.path.makeUrlAbsolute("../../foo/file.html", "http://foo.com/a/b/c/test.html");
 
// 返回: http://foo.com/bar/file.html
var absUrl = $.mobile.path.makeUrlAbsolute("//foo.com/bar/file.html", "http://foo.com/a/b/c/test.html");
 
// 返回: http://foo.com/a/b/c/test.html?a=1&b=2
var absUrl = $.mobile.path.makeUrlAbsolute("?a=1&b=2", "http://foo.com/a/b/c/test.html");
 
// 返回: http://foo.com/a/b/c/test.html#bar
var absUrl = $.mobile.path.makeUrlAbsolute("#bar", "http://foo.com/a/b/c/test.html");
Copy after login

十三. $.mobile.path.isSameDomain()比较两个 URL 的域。

具有两个参数 url1 (string, 可选) 和 url2 (string, 可选) 具体情况如下:

—— url1 (string, 可选)

一个相对 URL。

—— url2 (string, 可选)

url2 (string, required) 一个需要解析的绝对 URL 。

返回值为 boolean 型变量,若两个域匹配,则返回 "true" ,否则返回 "false" 。

例子:

// 返回: true
var same = $.mobile.path.isSameDomain("http://foo.com/a/file.html", "http://foo.com/a/b/c/test.html");
 
// 返回: false
var same = $.mobile.path.isSameDomain("file://foo.com/a/file.html", "http://foo.com/a/b/c/test.html");
 
// 返回: false
var same = $.mobile.path.isSameDomain("https://foo.com/a/file.html", "http://foo.com/a/b/c/test.html");
 
// 返回: false
var same = $.mobile.path.isSameDomain("http://foo.com/a/file.html", "http://bar.com/a/b/c/test.html");
Copy after login

十四. $.mobile.path.isRelativeUrl()判断一个 URL 是否是相对 URL 。

它具有一个参数 URL (string, 必选) ,其值为一个相对或绝对的 URL 。

返回值为 boolean 型变量,若 URL 为相对形式的 URL ,则返回 "true" ,否则返回 "false" 。

例子:

// 返回: false
var isRel = $.mobile.path.isRelativeUrl("http://foo.com/a/file.html");
 
// 返回: true
var isRel = $.mobile.path.isRelativeUrl("//foo.com/a/file.html");
 
// 返回: true
var isRel = $.mobile.path.isRelativeUrl("/a/file.html");
 
// 返回: true
var isRel = $.mobile.path.isRelativeUrl("file.html");
 
// 返回: true
var isRel = $.mobile.path.isRelativeUrl("?a=1&b=2");
 
// 返回: true
var isRel = $.mobile.path.isRelativeUrl("#foo");
Copy after login

十五. $.mobile.path.isAbsoluteUrl()判断一个 URL 是否是绝对 URL 。

它具有一个参数 URL (string, 必选) ,其值为一个相对或绝对的 URL 。

返回值为 boolean 型变量,若 URL 为绝对形式的 URL ,则返回 "true" ,否则返回 "false" 。

例子:

// 返回: true
var isAbs = $.mobile.path.isAbsoluteUrl("http://foo.com/a/file.html");
 
// 返回: false
var isAbs = $.mobile.path.isAbsoluteUrl("//foo.com/a/file.html");
 
// 返回: false
var isAbs = $.mobile.path.isAbsoluteUrl("/a/file.html");
 
// 返回: false
var isAbs = $.mobile.path.isAbsoluteUrl("file.html");
 
// 返回: false
var isAbs = $.mobile.path.isAbsoluteUrl("?a=1&b=2");
 
// 返回: false
var isAbs = $.mobile.path.isAbsoluteUrl("#foo");
Copy after login

十六. $.mobile.path.get()该方法可以判断一个 URL 的目录部分。如果 URL 末尾没有反斜杠,则 URL 最后的部分会被认为是文件名。这个情况对于站长来说应该不陌生,如 http://kayosite.com/aaa/ ,URL 中最后的部分 "aaa/" 应该是一个目录,而 http://kayosite.com/aaa/xxx.zip 中最后的部分 "xxx.zip" 则应该是一个文件名。这也是 Kayo 之前建议注意网址末尾是否需要添加反斜杠的原因。

这个方法具有一个参数 url (string, 必选) ,其值是一个相对或绝对的 URL 。

返回值为 URL 中的目录部分。

例子:

// 返回: http://foo.com/a/
var dirName = $.mobile.path.get("http://foo.com/a/file.html");
 
// 返回: http://foo.com/a/
var dirName = $.mobile.path.get("http://foo.com/a/");
 
// 返回: http://foo.com/a
var dirName = $.mobile.path.get("http://foo.com/a");
 
// 返回: //foo.com/a/
var dirName = $.mobile.path.get("//foo.com/a/file.html");
 
// 返回: /a/
var dirName = $.mobile.path.get("/a/file.html");
 
// 返回: ""
var dirName = $.mobile.path.get("file.html");
 
// 返回: /
var dirName = $.mobile.path.get("/file.html");
 
// 返回: ?a=1&b=2
var dirName = $.mobile.path.get("?a=1&b=2");
 
// 返回: foo
var dirName = $.mobile.path.isAbsoluteUrl("#foo");
Copy after login

十七. $.mobile.base获取根元素。

十八. $.mobile.silentScroll()静默滚动到某个 Y 值处,并且不触发任何事件。

它具有一个参数,yPos (number, 默认为 0),其值为需要滚动到的 Y 位置。

十九. $.mobile.activePage引用当前激活的页面。

二十. 关于方法的调用1. 方法调用相应 JavaScript 的引入

在介绍自定义 jQuery Mobile 默认配置时,曾经说明了相应的 JavaScript 语句需要放置在 jQuery 库和 jQuery Mobile 库之间,而 jQuery Mobile 方法是对 jQuery Mobile 的调用,因此需要在引入 jQuery Mobile 库之后调用,具体如下:

<script src="jquery.min.js"></script>
<!-- 引入自定义 jQuery Mobile 默认配置相应的 JavaScript -->
<script src="custom-mobile.js"></script>
<script src="jquery-mobile.min.js"></script>
<!-- 引入 jQuery Mobile 的调用,包括 jQuery Mobile 的方法、事件检测等全部应用性 JavaScript -->
<script src="my-site.js"></script>
Copy after login

2. 方法的调用

对于 jQuery 开发者,应该比较习惯在触发 ready 事件后执行 JavaScript ,例如:

$(document).ready(function(){
  // 执行 JavaScript
});
Copy after login

或简写为,

$(function(){
  // 执行 JavaScript
});
Copy after login

这里说明一下,当 DOM 已经加载,并且页面(包括图像)已经完全呈现出时,会触发 ready 事件。

而在系列文章的上一篇中,Kayo 介绍了 pageinit 事件,它在 DOM 加载完成后(包括 jQuery Mobile 对元素的 DOM 增强),就会触发,即它比 ready 更早的触发。

但由于 jQuery Mobile 驱动的网站由 Ajax 导航,因此即使一个文档中包含多个 'page' ,当第一个 'page' 的 DOM 和内容加载完毕后就会触发 ready 事件,而 pageint 也只需第一个 'page' 的 DOM 加载完毕后即触发。

而最终利用那种事件作为开始调用方法的合适时机,会涉及很多方便的考虑,开发者应该根据实际情况作出选择。

下面例举一个例子说明如何调用 jQuery Mobile 方法,由于 jQuery Mobile 的方法很多,这里只使用 $.mobile.changePage() 方法演示如何调用方法。例子中的 JavaScript 代码如下:

$(function(){
 
  $("#home").bind('swipeleft', function(){
 
    $.mobile.changePage('./page-2.html', {
      transition: "slide",
      role: "dialog"
    });
  });
});
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Mobile框架开发移动端Web App步骤详解

Jquery实现跨域异步上传文件步骤详解

The above is the detailed content of What functions does Mobile use to operate H5?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!