栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

这是使用History.js的正确方法吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

这是使用History.js的正确方法吗?

在完成更多工作之后,我提出了一个简单而完整的示例来说明如何使用最新的History.js。这是工作的jsfiddle示例,该示例执行Github上托管的 Ajax
HTML片段加载

<!DOCTYPE html><html>  <head>    <meta http-equiv="content-type" content="text/html; charset=UTF-8">    <title>Simple History.js Ajax example by dansalmo</title>    <script type="text/javascript" src="http://pre.jquery.com/jquery-1.8.2.js"></script>    <script type="text/javascript" src="http://balupton.github.com/history.js/scripts/bundled/html4+html5/jquery.history.js"></script>    <style type='text/css'>      .hidden {        display: none;        visibility: hidden;      }    </style>  </head>  <body>    <a href="/home">Home</a>    <a href="/about">about</a>    <a href="/contact">Contact</a>    <a href="/other">Other</a>    <p>The whole page will not re-load when the content below is updated, yet the URL is clean and the back button works!<p><br />    <div id="content">      <div id="home">Home Page content</div>    </div>    <br />    <p>The content div will be updated with a selected div fragment from an HTML file hosted on github, however the broswer will see each content update request as a part of the page history so that the back button can be used.</p>    <br  />    <p>Adding more menu items is as simple as adding the new links and their corresponding html fragments.<p>    <div id="hidden_content" ></div>  </body>    <script type='text/javascript'>//<![CDATA[   $(function(){  var History = window.History;  if (History.enabled) {      State = History.getState();      // set initial state to first page that was loaded      History.pushState({urlPath: window.location.pathname}, $("title").text(), State.urlPath);  } else {      return false;  }  var loadAjaxContent = function(target, urlbase, selector) {      $(target).load(urlbase + ' ' + selector);  };  var updateContent = function(State) {      var selector = '#' + State.data.urlPath.substring(1);    if ($(selector).length) { //content is already in #hidden_content        $('#content').children().appendTo('#hidden_content');        $(selector).appendTo('#content');    } else {         $('#content').children().clone().appendTo('#hidden_content');        loadAjaxContent('#content', State.url, selector);    }  };  // Content update and back/forward button handler  History.Adapter.bind(window, 'statechange', function() {      updateContent(History.getState());  });  // navigation link handler  $('body').on('click', 'a', function(e) {      var urlPath = $(this).attr('href');      var title = $(this).text();      History.pushState({urlPath: urlPath}, title, urlPath);      return false; // prevents default click action of <a ...>  });  });//]]>  </script></html>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号