2016-08-16 90 views
0

现在我有2个按钮,一个调用一个函数做某事,另一个在页面上显示信息。按钮调用函数而不更改URL或重新加载整个页面?

router.get('/run-func', function(res, req, next) { 
    //call function 
    res.render('index'); 
}; 

router.get('/get-data', function(res, req, next) { 
    //get info from db... 
    res.render('index', {items: tempArray}); 
}; 

一个按钮,点击它向我发送到另一个URL,但显示的索引页每一次:

http://localhost:3000/run-func

http://localhost:3000/get-data

的问题是我每次点击一次运行FUNC它重新加载页面,并且从get-data中显示的数据消失。有没有办法让这些按钮不加载不同的网址?

的HTML使用车把完成的,这里的获取数据之一:

<div class="get"> 
    <h3>Get Data</h3> 
    <a href="/get-data">LOAD DATA</a> 
    <div> 
     {{# each items }} 
      <article class="item"> 
       <div>id: {{ this._id }}</div> 
       <div>prop: {{ this.prop }}</div> 
      </article> 
     {{/each}} 
    </div> 
</div> 
+0

您发布的代码是服务器端。但是你描述的问题似乎是客户端。您应该添加按钮的客户端实现(HTML代码)。 – Cubius

回答

0

试试这个

function processAjaxData(response, urlPath){ 
 
    document.getElementById("content").innerHTML = response.html; 
 
    document.title = response.pageTitle; 
 
    window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath); 
 
}

然后可以使用window.onpopstate检测后退/前进按钮导航:

window.onpopstate = function(e){ 
 
    if(e.state){ 
 
     document.getElementById("content").innerHTML = e.state.html; 
 
     document.title = e.state.pageTitle; 
 
    } 
 
};

相关问题