按钮onClick事件和Href位置

按钮onClick事件和Href位置

本文介绍了按钮onClick事件和Href位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used a solution given by "Chrules" found in this discussion:buttons with no href, onclick etc

像这样:

<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()方法成功完成(对象已添加到数据库中)

So When i used the location.href like he putted it (= "http://www.takemehere.com";) The insertRecord() method is done with success (the object is added to the database)

但是当我使用location.href ="game.html"; game.html页面已打开,但insertRecord()方法未完成,我尝试放置game.html的完整路径,但仍然存在相同的问题,请问您有什么想法吗?预先谢谢你

But when I use the location.href = "game.html"; the game.html page is opened but the insertRecord() method is not done i tried to put the full path of game.html but the same problem persist Have you any idea please ? Thank u in advance

推荐答案

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

I think insertMethod() calls some async functions to do database updating. So you should use a callback before doing anything else.

类似这样的东西(我不知道insertRecord方法内部是什么);

Something like this (i don't know what is inside of insertRecord method);

function insertRecord(callback) {
  // do all sorts of database job
  // when it's finished
  // fire your callback
  callback();
}

insertRecord(function() {
  location.href = "page.html";
});

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

After your comment, i see you already have defined a function to run after sql which is loadAndReset(). So simply put location.href = "page.html" in that function, and it should work.

这篇关于按钮onClick事件和Href位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:21