{"id":129,"date":"2015-11-19T14:24:47","date_gmt":"2015-11-19T06:24:47","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=129"},"modified":"2019-01-18T18:40:29","modified_gmt":"2019-01-18T10:40:29","slug":"%e5%85%b3%e4%ba%8ejquery-mobile%e4%b8%ad%e8%bf%94%e5%9b%9e%e4%b8%8a%e7%ba%a7dom%e8%8a%82%e7%82%b9%e8%a2%ab%e5%86%bb%e7%bb%93%e5%88%a0%e9%99%a4%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=129","title":{"rendered":"\u5173\u4e8ejquery mobile\u4e2d&#8221;\u8fd4\u56de\u4e0a\u7ea7dom\u8282\u70b9\u88ab\u51bb\u7ed3\u5220\u9664&#8221;\u95ee\u9898\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<div class=\"blog-abstract\">\u6458\u8981: \u76f8\u6bd4\u5927\u5bb6\u90fd\u77e5\u9053jquery mobile\u4e2d\u7684\u5982\u4f55\u6dfb\u52a0&#8221;\u8fd4\u56de\u4e0a\u7ea7\u6309\u94ae&#8221;\uff0c \u4f46\u662f\u5f53\u8fd4\u56de\u7b2c\u4e09\u7ea7\u65f6\u5019DOM\u8282\u70b9\u4f1a\u88ab\u5220\u9664\uff0c\u5176\u4f59\u8282\u70b9\u51bb\u7ed3\u7684\u95ee\u9898\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5982\u4f55\u89e3\u51b3\u7684\uff0c\u4eca\u5929\u5206\u4eab\u4e00\u4e0b\u6211\u7684\u4e2a\u4eba\u5fc3\u5f97<\/div>\n<div id=\"blogBody\" class=\"blog-body\">\n<div class=\"BlogContent\">\n<p>jQuery Mobile \u662f\u7528\u4e8e\u521b\u5efa\u79fb\u52a8 Web \u5e94\u7528\u7684\u524d\u7aef\u5f00\u53d1\u6846\u67b6\u3002<\/p>\n<p>jQuery Mobile \u53ef\u4ee5\u5e94\u7528\u4e8e\u667a\u80fd\u624b\u673a\u4e0e\u5e73\u677f\u7535\u8111\u3002<\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u5047\u8bbe\u5728\u9879\u76ee\u4e2d\uff0c\u6709\u4e09\u4e2a\u9875\u9762\uff0c\u5206\u522b\u662fmain.html\u3001test1.html\u3001test2.html\uff08\u540e\u9762\u5206\u522b\u7b80\u79f0main\u3001test1\u3001 test2\uff09\uff0c\u5176\u4e2dmain\u9875\u9762\u662f\u5305\u542b\u4e00\u4e2a\u8f6c\u5411\u5230test1\u9875\u9762\u7684\u94fe\u63a5\uff08\u5373a\u6807\u7b7e\uff09\uff0ctest1\u4e2d\u6709\u4e00\u4e2a\u5c5e\u6027\u4e3adata-rel=\u201dback\u201d\u7684\u94fe\u63a5\u548c\u4e00 \u4e2a\u8f6c\u5411\u5230test2\u7684\u94fe\u63a5\uff0ctest2\u53ea\u6709\u4e00\u4e2a\u5c5e\u6027\u4e3adata-rel=\u201dback\u201d\u7684\u94fe\u63a5\u3002main\u8f6c\u5411\u5230test1\u540e\uff0c\u70b9\u51fbback\u94fe\u63a5\u8fd4\u56de main\uff08\u76f8\u5f53\u4e8e\u70b9\u51fb\u6d4f\u89c8\u5668\u7684\u8fd4\u56de\u6309\u94ae\uff09\uff0c\u65e0\u9700\u91cd\u65b0\u53d1\u9001get\u8bf7\u6c42\uff1b\u4f46\u662f\u5f53test1\u8f6c\u5411\u5230test2\uff0c\u5728test2\u9875\u9762\u70b9\u51fbback\u94fe\u63a5\u60f3\u8fd4\u56de test1\u65f6\uff0c\u4f1a\u91cd\u65b0\u53d1\u9001\u4e00\u4e2aget\u8bf7\u6c42\u3002\u8fd9\u6837\u5bfc\u81f4\u7684\u95ee\u9898\u5c31\u662f\uff1atest1\u505a\u7684\u6240\u6709\u64cd\u4f5c\uff0c\u5728test2\u8fd4\u56de\u540e\uff0c\u90fd\u4f1a\u5931\u6548\u3002\u6bd4\u5982A\u662f\u4e00\u4e2a\u5206\u9875\u7684\u5217\u8868\u9875\u9762\uff0c\u7ffb \u5230\u7b2c\u4e8c\u9875\u540e\u8f6c\u5411\u5230B\uff0c\u90a3\u4e48\u5f53\u8fd4\u56deA\u540e\uff0c\u5c31\u65e0\u6cd5\u5730\u4f4d\u5230\u7b2c\u4e8c\u9875\u3002<\/p>\n<p><strong>\u539f\u56e0\u5206\u6790<\/strong><\/p>\n<p>\u6211\u9996\u5148\u7528firebug\u770b\u4e86\u4e00\u4e0bhtml\u7684\u7ed3\u6784\uff0c\u53d1\u73b0jQuery Mobile\u4f1a\u628amain\u548ctest1\u52a0\u5165\u5230\u9875\u9762\u7ed3\u6784\u4e2d\u53bb\uff0c\u5f53\u4ecetest1\u8f6c\u5411\u5230test2\u540e\uff0ctest1\u4f1a\u88ab\u81ea\u52a8\u5220\u9664\uff0c\u8fd9\u6837dom\u6811\u4e2d\uff0c\u53ea\u5305\u542b\u4e86 main\u548ctest2\uff0c\u6240\u4ee5\u5728test2\u8fd4\u56detest1\u5c31\u4f1a\u5728\u53d1\u9001\u4e00\u4e2aget\u8bf7\u6c42\u3002\u90a3\u4e48\u662f\u4e0d\u662f\u610f\u5473\u7740\uff0c\u53ea\u8981\u80fd\u628a\u5386\u53f2\u9875\u9762\u7f13\u5b58\u5230dom\u4e2d\uff08\u5c31\u50cfmain\u548c test1\u4e00\u6837\uff09\uff0c\u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p><strong>\u89e3\u51b3\u95ee\u9898<\/strong><\/p>\n<p>\u7ecf\u8fc7\u4e00\u756a\u67e5\u627e\uff0c\u5728jQuery Mobile\u5b98\u7f51\u770b\u5230\u4e00\u6bb5\u300aCaching pages in the DOM\u300b\u7684\u63cf\u8ff0\uff1a<\/p>\n<p>Caching pages in the DOM<br \/>\nTo keep all previously-visited pages in the DOM, set the domCache option on the page plugin to true, like this:<br \/>\n$.mobile.page.prototype.options.domCache = true;<br \/>\nAlternatively, to cache just a particular page, you can add the data-dom-cache=&#8221;true&#8221; attribute to the page&#8217;s container:<br \/>\n&lt;div data-role=&#8221;page&#8221; id=&#8221;cacheMe&#8221; data-dom-cache=&#8221;true&#8221;&gt;<br \/>\nYou can also cache a page programmatically like this:<br \/>\npageContainerElement.page({ domCache: true });<br \/>\nThe drawback of DOM caching is that the DOM can get very large, resulting in slowdowns and memory issues on some devices. If you enable DOM caching, take care to manage the DOM yourself and test thoroughly on a range of devices.<\/p>\n<p>\u4ece\u8fd9\u6bb5\u5f15\u6587\u4e2d\u5e94\u8be5\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u4e09\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7f13\u5b58\u9875\u9762\u5230dom\u4e2d\uff0c\u4e8e\u662f\u6211\u5c31\u4f7f\u7528\u4e86\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff0c\u5373\u5728page\u7684div\u4e0a\u589e\u52a0\u4e86data-dom-cache=\u201dtrue\u201d\u5c5e\u6027\uff0c\u4f46\u662f\u5374\u51fa\u73b0\u4e86\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n<p>1\u3001\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5f53\u6211\u7684\u8bbf\u95ee\u8def\u5f84\u662fmain-&gt;test1-&gt;test2-&gt;test1\uff08test2\u662f history.back()\u8fd4\u56de\u7684\uff09\u65f6\uff0c\u7528firebug\u53ef\u4ee5\u770b\u5230\uff0ctest2\u4ecd\u7136\u5b58\u5728\u4e8edom\u4e2d\uff0c\u8fd9\u6837\u7684\u7ed3\u679c\u5c31\u5982\u7ea2\u8272\u90e8\u5206\u63cf\u8ff0\u7684\uff1aDOM\u4f1a\u53d8\u5f97\u5f88\u5927\uff0c \u7ed3\u679c\u4f1a\u4f7f\u9875\u9762\u53d8\u6162\u548c\u4e00\u4e9b\u8bbe\u5907\u4e0a\u7684\u5185\u5b58\u9519\u8bef\u3002<\/p>\n<p><img loading=\"lazy\" src=\"http:\/\/static.oschina.net\/uploads\/space\/2016\/0702\/154003_sn9W_2760473.jpg\" alt=\"\" width=\"829\" height=\"556\" \/>2\u3001\u5f53\u6211\u5b58\u5728\u8fd9\u6837\u4e00\u4e2a\u9875\u9762\uff0c\u5b83\u901a\u8fc7\u4e0d\u540c\u7684\u53c2\u6570\u663e\u793a\u4e0d\u540c\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u9875\u9762\u4e0a\uff0c\u6709\u4e00\u6bb5js\u811a\u672c\uff0c\u4f1a\u5bf9\u9875\u9762\u4e0a\u7684\u5143\u7d20\u505a\u4e9b\u5904\u7406\uff0c\u800c\u6211\u4eec\u5e38\u7528\u7684\u65b9\u5f0f\u5c31\u662f\u7528id\u6765\u83b7\u5f97 \u76ee\u6807\u5143\u7d20\uff0c\u7531\u4e8e\u6211\u4eec\u662f\u7528\u4e86cache\u7f13\u5b58page\uff0c\u5c31\u4f1a\u5bfc\u81f4js\u4e8b\u4ef6\u6216\u8005\u64cd\u4f5c\u6df7\u4e71\u3002\u6bd4\u5982\u5728\u8fd9\u91cc\u6211\u589e\u52a0\u4e86\u4e00\u4e2atest1_1\u9875\u9762\uff0c\u5b83\u7684\u5185\u5bb9\u51e0\u4e4e\u548ctest1 \u4e00\u6837\uff0c\u4ed6\u4eec\u90fd\u6709\u76f8\u540cid\u7684div\u548c\u76f8\u540c\u4e8b\u4ef6\u5904\u7406\u7684button\uff0c\u8fd9\u4e2a\u4e8b\u4ef6\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u5f80\u8fd9\u4e2adiv\u4e2d\u589e\u52a0\u5185\u5bb9\uff0c\u5f53\u8bbf\u95ee\u8def\u5f84\u4e3a main-&gt;test1-&gt;test1_1\uff0c\u5728test1_1\u4e0a\u70b9\u51fb\u6309\u94ae\uff0c\u4f1a\u53d1\u73b0\u597d\u50cf\u6ca1\u6709\u89e6\u53d1\u8fd9\u4e2a\u4e8b\u4ef6\uff0c\u5176\u5b9e\u5b83\u5df2\u7ecf\u89e6\u53d1\u4e86\uff0c\u53ea\u662f\u5185\u5bb9\u589e\u52a0\u5230 test1\u4e2d\u7684div\u4e2d\u4e86\uff0c\u5206\u522b\u5165\u4e0b\u56fe<\/p>\n<p>&nbsp;<\/p>\n<p><img src=\"http:\/\/static.oschina.net\/uploads\/space\/2016\/0702\/154042_AovJ_2760473.jpg\" alt=\"\" \/><\/p>\n<p>\u6240\u4ee5\u5bf9\u4e8e\u76ee\u524d\u5927\u591a\u6570\u5e94\u7528\uff0c\u8fd9\u4e2a\u65b9\u6848\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u9664\u975e\u81ea\u5df1\u7ba1\u7406dom\u4e2d\u9875\u9762\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<p><strong>\u4f18\u5316\u65b9\u6848<\/strong><\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u5b9e\u9a8c\uff0c\u6211\u4e5f\u77e5\u9053\u4e86\u9700\u8981\u6ee1\u8db3\u6211\u7684\u9700\u6c42\uff0c\u5c31\u53ea\u80fd\u81ea\u5df1\u7ba1\u7406dom\u4e2d\u9875\u9762\u7684\u751f\u547d\u5468\u671f\u4e86\u3002\u90a3\u4e48\u5c31\u6d89\u53ca\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\u9875\u9762\u4ec0\u4e48\u65f6\u5019\u8fc7\u671f\uff08\u5373\u4ecedom\u4e2d\u5220 \u9664\uff09\u5462\uff1f\u6839\u636e\u6211\u7684\u9700\u6c42\uff0c\u5f53\u4ecetest2\u8fd4\u56de\u5230test1\u65f6\uff0c\u5c31\u5e94\u8be5\u4ecedom\u4e2d\u5220\u9664test2\uff0c\u540c\u7406\u4ecetest1\u8fd4\u56demain\u65f6\uff0c\u4ecedom\u4e2d\u5220\u9664 test1\u3002\u5982\u679c\u518d\u6b21\u4ecemain\u5bfc\u822a\u5230test1\uff0c\u5c31\u5f97\u53d1\u8d77\u4e00\u4e2aget\u8bf7\u6c42\uff0c\u6211\u8ba4\u4e3a\u8fd9\u662f\u5408\u7406\u7684\uff0c\u56e0\u4e3a\u7528\u6237\u4e0d\u4f1a\u8ba4\u4e3a\u70b9\u51fb\u94fe\u63a5\u5230\u65b0\u9875\u9762\u8fd8\u9700\u8981\u7f13\u5b58\u3002\u6240\u4ee5\u6211\u5e94\u8be5 \u5728\u9875\u9762\u663e\u793a\u524d\u6216\u8005\u663e\u793a\u540e\uff0c\u5220\u9664\u5b83\u4e4b\u540e\u7684history\uff0c\u4e8e\u662f\u6211\u5c31\u5728pagebeforeshow\u3001pageshow\u7684\u65f6\u5019\u505a\u4e86\u5220\u9664\u64cd\u4f5c\uff0c\u5373\u5982\u4e0b\u811a\u672c\uff08\u63d2\u4ef6 \u5f62\u5f0f\uff09\uff1a<\/p>\n<p><code>(<\/code><code>function<\/code><code>($, undefined) {<\/code><\/p>\n<p><code>$.fn.subpage = <\/code><code>function<\/code><code>(options) {<\/code><\/p>\n<p><code>$(document).bind(<\/code><\/p>\n<p><code>\"pagebeforeshow\"<\/code><code>,<\/code><\/p>\n<p><code>function<\/code><code>() {<\/code><\/p>\n<p><code>var<\/code> <code>forword = $.mobile.urlHistory.getNext();<\/code><\/p>\n<p><code>if<\/code> <code>(forword) {<\/code><\/p>\n<p><code>var<\/code> <code>dataUrl = forword.url;<\/code><\/p>\n<p><code>var<\/code> <code>forwordPage=$.mobile.pageContainer<\/code><\/p>\n<p><code>.children(<\/code><code>\":jqmData(url='\"<\/code> <code>+ dataUrl + <\/code><code>\"')\"<\/code><code>);<\/code><\/p>\n<p><code>if<\/code><code>(forwordPage){<\/code><\/p>\n<p><code>forwordPage.remove();<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>$.mobile.urlHistory.clearForward();<\/code><\/p>\n<p><code>});<\/code><\/p>\n<p><code>};<\/code><\/p>\n<p><code>$(document).bind(<\/code><code>\"pagecreate create\"<\/code><code>, <\/code><code>function<\/code><code>(e) {<\/code><\/p>\n<p><code>$(<\/code><code>\":jqmData(role='page')\"<\/code><code>, e.target).subpage();<\/code><\/p>\n<p><code>});<\/code><\/p>\n<p><code>})(jQuery);<\/code><\/p>\n<p>\u7ed3\u679c\u4e8b\u4e0e\u613f\u8fdd\uff0c\u5728\u9875\u9762\u8fd4\u56de\u65f6\uff0c\u51fa\u73b0\u4e86js\u811a\u672c\u9519\u8bef\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img src=\"http:\/\/static.oschina.net\/uploads\/space\/2016\/0702\/154218_r5gY_2760473.jpg\" alt=\"\" \/><\/p>\n<p>\u90a3\u4e48\u662f\u4ec0\u4e48\u539f\u56e0\u5462\uff1f\u4e0d\u5728\u8fd9\u4e2a\u4e8b\u4ef6\u91cc\u505a\u5904\u7406\uff0c\u90a3\u5728\u54ea\u91cc\u5904\u7406\u5462\uff1f\u4e8e\u662f\u6211\u4ed4\u7ec6\u7814\u8bfb\u4e86\u4e00\u4e0bjQuery Mobile\u6e90\u7801\uff0c\u53d1\u73b0\u4e86\u4e0b\u9762\u4e00\u6bb5\uff1a<\/p>\n<p><a href=\"http:\/\/www.jb51.net\/article\/83024.htm#\" rel=\"nofollow\">?<\/a><\/p>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<p>4<\/p>\n<p>5<\/p>\n<p>6<\/p>\n<p>7<\/p>\n<p>8<\/p>\n<p>9<\/p>\n<p>10<\/p>\n<p>11<\/p>\n<p>12<\/p>\n<p>13<\/td>\n<td><code>transitionPages( toPage, fromPage, settings.transition, settings.reverse )<\/code><\/p>\n<p><code>.done(<\/code><code>function<\/code><code>() {<\/code><\/p>\n<p><code>removeActiveLinkClass();<\/code><\/p>\n<p><code>\/\/if there's a duplicateCachedPage, remove it from the DOM now that it's hidden<\/code><\/p>\n<p><code>if<\/code> <code>( settings.duplicateCachedPage ) {<\/code><\/p>\n<p><code>settings.duplicateCachedPage.remove();<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>\/\/remove initial build class (only present on first pageshow)<\/code><\/p>\n<p><code>$html.removeClass( <\/code><code>\"ui-mobile-rendering\"<\/code> <code>);<\/code><\/p>\n<p><code>releasePageTransitionLock();<\/code><\/p>\n<p><code>\/\/ Let listeners know we're all done changing the current page.<\/code><\/p>\n<p><code>mpc.trigger( <\/code><code>\"pagechange\"<\/code><code>, triggerData );<\/code><\/p>\n<p><code>});<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9875\u9762\u5728\u5207\u6362\u5b8c\u540e\uff0c\u4f1a\u89e6\u53d1pagechange\u4e8b\u4ef6\uff0c\u4e8e\u662f\u6211\u628apagebeforeshow\u6539\u6210\u4e86pagechange\uff0c\u4e00\u5207\u90fd\u6309\u9884\u671f\u8fd0\u884c\uff0c\u5e76\u4e14\u6ca1\u6709\u9519\u8bef\uff0c\u7ec8\u4e8e\u5927\u529f\u544a\u6210\u4e86\u3002<\/p>\n<p>\u5728\u4f7f\u7528\u8be5\u63d2\u4ef6\u65f6\uff0c\u8bf7\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<p>1\u3001\u5fc5\u987b\u5728\u5f15\u7528\u8be5\u811a\u672c\u4e4b\u524d\uff0c\u5f15\u7528jquery\u548cjquery mobile\u811a\u672c\u6587\u4ef6\uff1b<\/p>\n<p>2\u3001\u5fc5\u987b\u5728page\u4e0a\u589e\u52a0data-dom-cache=&#8221;true&#8221;\u3002<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981: \u76f8\u6bd4\u5927\u5bb6\u90fd\u77e5\u9053jquery mobile\u4e2d\u7684\u5982\u4f55\u6dfb\u52a0&#8221;\u8fd4\u56de\u4e0a\u7ea7\u6309\u94ae&#8221;\uff0c \u4f46\u662f\u5f53 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/129"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=129"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":130,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/129\/revisions\/130"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}