director.js 路由

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>director.js</title>
    <style type="text/css">
    .test {width: 100px;height: 100px;}
    span {cursor: pointer;}
    </style>
  </head>

  <body>
    <ul>
      <li><a href="#/author">#/author</a></li>
      <li><a href="#/books">#/books</a></li>
      <li><span href="#/books/view/1_1">#/books/view/1</span></li>
      <li><span href="#/books/view/2_2">#/books/view/2</span></li>
      <li><span href="#/books/view/3_3">#/books/view/3</span></li>
      <li><a href="https://www.baidu.com/?tn=47018152_dg">百度</a></li>
      <div class="test"></div>
    </ul>

    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
    <script src="https://cdn.bootcss.com/Director/1.2.8/director.js"></script>
    <script>
      var author = function () {
          //console.log("author");
          $('.test').css({background:'green'});
          };
      var books = function () {
          //console.log("books");
          $('.test').css({background:'pink'});
      };
      var viewBook = function (bookId) {
        //console.log("viewBook: bookId is populated: " + bookId);
        var id = bookId.split('_')[0];
        var tid = bookId.split('_')[1];
        if (id == 1) {
            $('.test').css({background:'gold'});
            console.log(tid);
        } else if  (id == 2) {
            $('.test').css({background:'#dddddd'});
            console.log(tid);
        } else if (id == 3) {
            $('.test').css({background:'blue'});
            console.log(tid);
        }
      };

      $('span').on('click', function() {
          var temp = location.href.split('#')[0];
          location.href = temp + $(this).attr('href');
      });

      var routes = {
        '/author': author,
        '/books': [books, function() {
          console.log("An inline route handler.");
        }],
        '/books/view/:bookId': viewBook
      };
      var router = Router(routes);
      router.init();
    </script>
  </body>
</html>

通过js控制跳转:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>A Gentle Introduction</title>
 
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
    <script src="https://cdn.bootcss.com/Director/1.2.8/director.min.js"></script> 
 
    <script>
    $('document').ready(function() {
      //
      // create some functions to be executed when
      // the correct route is issued by the user.
      //
      var showAuthorInfo = function () { console.log("showAuthorInfo"); };
      var listBooks = function () { console.log("listBooks"); };
 
      var allroutes = function() {
        var route = window.location.hash.slice(2);
        var sections = $('section');
        var section;
 
        section = sections.filter('[data-route=' + route + ']');
 
        if (section.length) {
          sections.hide(250);
          section.show(250);
        }
      };
 
      //
      // define the routing table.
      //
      var routes = {
        '/author': showAuthorInfo,
        '/books': listBooks
      };
 
      //
      // instantiate the router.
      //
      var router = Router(routes);
 
      //
      // a global configuration setting.
      //
      router.configure({
        on: allroutes
      });
 
      router.init();

      $('#m-author').on('click', function () {
        window.location = '#/author';  
      });
      $('#m-books').on('click', function () {
        window.location = '#/books';  
      });
    });
    </script> 
  </head>
 
  <body>
    <section data-route="author">Author Name</section>
    <section data-route="books">Book1, Book2, Book3</section>
    <ul>
      <li><a href="javascript:;" id="m-author">author</a></li>
      <li><a href="javascript:;" id="m-books">books</a></li>
    </ul>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值