如何创建一个按钮,将我带到我网站上某个页面的1/4,同时将我带到我网站上第二页的第4页?理想情况下,它会选择1-4之间的随机整数,如果选择1,它将转到第一页,选择2,3或4,它将转到第二页。如果我要将一行或两行代码投入到创建按钮的网站创建者中,HTML代码会是什么样子?
答案 0 :(得分:0)
请参阅以下完整的HTML示例,我试图让它尽可能简单 -
您只需在页面底部添加javascript,然后将脚本中的submitBtn ID更改为现有页面上按钮的ID。
<html>
<head></head>
<body>
<button id='submitBtn'>Submit</button>
</body>
<script>
//get the button by the ID, your ID may differ
var submitBtn = document.getElementById('submitBtn');
//define the action you want to take when the button is clicked
submitBtn.addEventListener('click', function(){
//set the min and max values for the random number to be between
var maximum = 4;
var minimum = 1;
//get the random number
var randomnumber = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
//do your logic on the random number
if(randomnumber == 1){
window.location.href = 'http://urlwhennumberis1.com';
}
else{
window.location.href = 'http://urlforanyothernumber.com';
}
});
</script>
</html>