选择警报框选项时页面跳转

时间:2019-05-12 06:40:05

标签: javascript html

我希望根据用户在警报框中的选择,将用户页面跳转到我网站的固定位置。当警报框弹出时,如果他们确认自己拥有iPhone(这只是信息性的,设备确实没有关系),则应说“很酷”并留在原处,因为iPhone信息从顶部开始。但是,如果取消它们,我希望它们被页面跳转到Android信息开始的页面底部。

我一直在浏览堆栈溢出和其他各种站点,但是找不到解决这个问题的方法,但是如果我错了,请原谅。

<!--the Javascript function -->

function Question() {
var x=window.confirm("You have an iPhone right???")
if (x)
window.alert("Good!")
else
window.alert("Here is info for your device type!")
}

<!-- the html im looking to jump to -->

<h2 id = galaxyInt><a id = "SamsungStart">Samsung Galaxy Mobile 
Security</a></h2>

我希望不单击警报框确认,否则else语句会将用户从页面跳到适当的部分。

1 个答案:

答案 0 :(得分:0)

使用window.location.href =“ #SamsungStart”;链接到您要访问的ID。

<!--the Javascript function -->

function Question() {
var x=window.confirm("You have an iPhone right???")
if (x)
window.alert("Good!")
else
window.location.href = "#SamsungStart";
}
Question();
#top{
width:100%;
height:300px;
float:left;
background-color:blue;
}
.container2{
width:100%;
height:400px;
float:left;
background-color:green;

}
#galaxyInt{
width:100%;
float:left;

}
#SamsungStart{
width:100%;
float:left;
}
<!-- the html im looking to jump to -->

<div id = "top"></div>



<div class="container2">
<h2 id = "galaxyInt"><a id = "SamsungStart">Samsung Galaxy Mobile 
Security</a></h2></div>