Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 更改为davis.js路由库_Jquery - Fatal编程技术网

Jquery 更改为davis.js路由库

Jquery 更改为davis.js路由库,jquery,Jquery,我使用pushstate/popstate构建了一个演示,但我想知道如果Davis.js路由库在这里如何使用,有人能帮我做下面的例子吗? 谢谢 使用davis.jsindex.php print"<a class=\"a\" href=\"$result[id]\"></a>"; if($_GET['id']){ if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ pr

我使用pushstate/popstate构建了一个演示,但我想知道如果Davis.js路由库在这里如何使用,有人能帮我做下面的例子吗?
谢谢

使用davis.jsindex.php

print"<a class=\"a\" href=\"$result[id]\"></a>";
if($_GET['id']){
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
        print"
            <div class=\"wrapb\">
                <div class=\"b\"></div>
                <div class=\"close\"></div>
            </div>
        ";
    }
}

我认为这应该做你想做的事情:

var app = Davis(function () {
  this.get('/', function (req) {
    var xhr = $.ajax('/', {
      data: { id: req.params.id },
      dataType: 'html'
    })

    xhr.then(function (data) {
      var html = $(data)

      html.find('.close').on('click', function () {
        history.back()
      })

      $('.a').before(html)
    })
  })
})
生成给路由处理程序的
req
对象不是ajax请求,它是一个表示路径的“请求”的对象,在本例中是
/
,更多来自

当在路由回调中单击链接时,您必须执行任何您希望执行的工作,在您的情况下,看起来您希望向服务器发出一个请求,该请求将以剪下的html响应(我不是PHP开发人员,因此这可能是错的)。然后,您希望在当前文档中的链接后附加此html

Davis的设计不允许基于查询参数的路由,因此您不应该在路径定义中使用它们,因此我将其更改为仅
/

var app = Davis(function () {
  this.get('/', function (req) {
    var xhr = $.ajax('/', {
      data: { id: req.params.id },
      dataType: 'html'
    })

    xhr.then(function (data) {
      var html = $(data)

      html.find('.close').on('click', function () {
        history.back()
      })

      $('.a').before(html)
    })
  })
})