按钮单击事件和 Href 位置

Button onClick event and Href-Location

本文关键字:Href 位置 事件 单击 按钮      更新时间:2023-09-26

我使用了在此讨论中找到的"Chrules"给出的解决方案:没有href的按钮,onclick等

喜欢这个:

<button id="clickable" >Click Me!!</button>
 ......
$("#clickable").click(function(){
 // Send user to this link
   insertRecord(); //SQLite Code
  location.href = "game.html";
   // location.href = "http://www.takemehere.com"; });

所以当我使用 location.href 就像他放的那样(= "http://www.takemehere.com";)insertRecord() 方法成功完成(对象被添加到数据库中)

但是当我使用 location.href = "game.html"; 游戏.html页面打开但 insertRecord() 方法未完成时,我试图放置游戏的完整路径.html但同样的问题仍然存在 你知道吗?提前谢谢你

我认为insertMethod()调用一些异步函数来进行数据库更新。因此,您应该在执行其他任何操作之前使用回调。

像这样的东西(我不知道插入记录方法里面有什么);

function insertRecord(callback) {
  // do all sorts of database job
  // when it's finished
  // fire your callback
  callback();
}
insertRecord(function() {
  location.href = "page.html";
});

编辑:在您的评论之后,我看到您已经定义了一个在 sql 之后运行的函数,该函数loadAndReset()。因此,只需将location.href = "page.html"放入该函数中,它应该可以工作。