【发布时间】:2020-10-12 21:17:15
【问题描述】:
我希望了解在浏览器中切换页面时方法的差异。我的理解如下:
- 锚标签
<a href = "#">...</a>可用于使用文本和图片(非按钮)重定向页面 - 按钮元素
<button id = "go" onClick = "fx()">具有可以触发下一步操作的属性
以下两个按钮都应写入您的控制台日志: 点击一个
function fx(){
console.log("Thanks for taking the time to review my post!");
}
<button onClick="fx()">Click One</button>
点击两个
$("#go").on("click", function(e){
//this callback keeps the screen steady then calls an AJAX function
e.preventDefault();
fakeAJAX("/gone", function(resp){
//$.get("/gone", function(resp){
//this callback delivers the response from the AJAX.get method
console.log(resp);
});
});
function fakeAJAX(words, funct){
//this function is meant to simulate modules.export = fx(app){
//it should respond with the result of anything matching AJAX call
if(words == "/gone"){
//this would be app.get("/gone", function(req, res){
console.log("Your input helps me become a better #dev!");
//this would be res.send("...");
}//closing out app.get
}//} closing out modules.export simulation
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="go">Click Two</button>
在最近的一次练习中,我在使用按钮切换页面时遇到了困难...我希望能够使用 res.send(?) 格式(在 Click Two 示例中演示)返回的页面我想要,但到目前为止还没有奏效;我得到的最好结果是目标页面中的整个 html 在浏览器控制台中以纯文本形式打印...(我将添加指向该问题的链接)
其他开发人员的建议是使用超链接和 window.location 更改页面;我可以承认我已经成功地使用这两个建议中的后者更改了页面,但我想知道为什么我的首选方法被规避了。如果你们中的任何一个可以回答这些,你会帮助我了解如何使用这些换页方法
- 使用 AJAX 调用返回目标页面的“成本”是多少?
- 在工作的#dev 社区中如何看待 AJAX 方法与超链接和 window.location?
- 在哪些情况下,这些方法中的每一种都比其他方法更有意义?它们什么时候做得太多或做得不够?
【问题讨论】:
标签: jquery ajax express button response