JS生成随机1.html 2.html 3.html地址并跳转
时间: 2024-01-05 22:44:41 浏览: 23
以下是JS生成随机地址并跳转的代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>随机跳转</title>
</head>
<body>
<script type="text/javascript">
// 生成1-3之间的随机整数
var randomNumber = Math.floor(Math.random() * 3) + 1;
// 根据随机数生成对应的地址
var url = randomNumber + ".html";
// 跳转到随机生成的地址
window.location.href = url;
</script>
</body>
</html>
```
该代码会生成一个随机的整数(在1-3之间),然后根据随机数生成对应的地址(例如,如果随机数为1,则生成地址为1.html),最后使用`window.location.href`将页面跳转到生成的地址。
相关问题
写一个完整的随机跳转链接html代码给我,自动随机跳转到三条链接的任意一条里面。页面只显示正在为您规划路线。。。。。
好的,下面是代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>随机跳转链接</title>
<meta http-equiv="refresh" content="0;url=http://www.link1.com">
<script>
var urls = [
'http://www.link1.com',
'http://www.link2.com',
'http://www.link3.com'
];
var randomIndex = Math.floor(Math.random() * urls.length);
var randomUrl = urls[randomIndex];
window.location.href = randomUrl;
</script>
</head>
<body>
正在为您规划路线......
</body>
</html>
```
解释一下代码:
- `<meta http-equiv="refresh" content="0;url=http://www.link1.com">` 这一行代码的作用是:在页面加载完成后立即跳转到指定的链接。我们将它的 `content` 属性设置为 0,表示立即跳转。
- `<script>` 标签内的代码是 JavaScript 代码,用来实现随机跳转。它首先定义了一个数组 `urls`,里面存放了三个链接。然后通过 `Math.random()` 方法生成一个 0 到 1 之间的随机数,再将它乘上 `urls.length`,取整后得到一个随机的数组下标 `randomIndex`。最后使用 `window.location.href` 将页面跳转到对应的链接。
在页面中间加上“正在为您规划路线......”这句话,就可以了。
html超链接词云代码示例
这里给出一个使用Python和WordCloud库生成HTML超链接词云的示例代码:
```python
from wordcloud import WordCloud
import random
# 生成词云
text = "Python Java HTML CSS JavaScript Ruby PHP"
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
# 根据词频随机生成颜色
def random_color(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
hue = random.randint(0, 255)
saturation = random.randint(0, 255)
lightness = random.randint(0, 255)
return "hsl({}, {}%, {}%)".format(hue, saturation, lightness)
# 生成HTML超链接词云
html = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML超链接词云</title>
<style>
.wordcloud {
width: 800px;
height: 400px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="wordcloud">
{}
</div>
</body>
</html>
'''
links = ['https://www.python.org/', 'https://www.java.com/', 'https://www.w3.org/html/', 'https://www.w3.org/Style/CSS/', 'https://www.javascript.com/', 'https://www.ruby-lang.org/', 'https://www.php.net/']
words = wordcloud.words_
for word in words:
link = links[random.randint(0, len(links)-1)]
words[word] = '<a href="{}" target="_blank" style="color:{}">{}</a>'.format(link, random_color(), word)
html = html.format(wordcloud.to_html().replace("class=\"wordcloud\"", ""))
# 输出HTML代码
print(html)
```
运行上述代码后,会生成一个HTML词云页面,每个词都会变成一个超链接,点击后会跳转到对应的网址。你可以自己替换`text`变量和`links`列表中的内容,生成你自己的词云。