我有三个按钮,每个按钮都在
一切都正常。我点击按钮,函数被调用,传递了正确的字符串,然后我愉快地被发送到下一个页面,那里一切也很正常。但是,我必须点击两次按钮才能使其工作。第一次单击不起作用。有什么想法吗?
我已经在Stack Overflow上研究了这个问题,但无法通过this和this问题解决我的问题。
onclick
中调用chooseMap()
函数,然后根据按钮的id
重定向用户到一个新页面。一切都正常,但每次都需要点击两次。有人能告诉我为什么会这样,以及如何解决吗?<div class="row text-center" onclick="chooseMap();">
<div class="col-md-4">
<button type="button" class="btn btn-primary" id="world" >World Map</button>
</div>
<div class="col-md-4">
<button type="button" class="btn btn-primary" id="europe">Europe</button>
</div>
<div class="col-md-4">
<button type="button" class="btn btn-primary" id="sweden">Sweden</button>
</div>
</div>
function chooseMap(){
$(document).ready(function() {
document.getElementById("sweden").onclick = function(){
location.href = "map_game/sweden.html";
}
document.getElementById("europe").onclick = function(){
location.href="map_game/europe.html";
}
document.getElementById("world").onclick = function(){
location.href="map_game/world.html";
}
})
}
一切都正常。我点击按钮,函数被调用,传递了正确的字符串,然后我愉快地被发送到下一个页面,那里一切也很正常。但是,我必须点击两次按钮才能使其工作。第一次单击不起作用。有什么想法吗?
我已经在Stack Overflow上研究了这个问题,但无法通过this和this问题解决我的问题。
document.ready
放在chooseMap
函数内部? - Amit Soni