【发布时间】:2020-12-03 22:40:02
【问题描述】:
我有 3 个要链接在一起的 html 文件。这三个文件分别是button.html、option1.html、option2.html,三个文件都存放在一个src文件夹中。
button.html 是一个包含两个按钮的简单网页:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script type="text/javascript">
document.getElementById("option1").onclick = function () {
location.href = "./option1.html";
};
document.getElementById("option2").onclick = function () {
location.href = "./option2.html";
};
</script>
</head>
<body>
<button type="button" id="option1">Option 1</button>
<button type="button" id="option2">Option 2</button>
</body>
</html>
另外两个 .HTML 文件是常规页面,每个页面都有不同的内容。
<!DOCTYPE html>
<html>
<head>
<title>Option 1/2</title>
</head>
<body>
// different data contained for option1.html and option2.html
<h1>Heading for Option 1 or 2</h1>
<p>Paragraph for Option 1 or 2</p>
</body>
</html>
我不确定我做错了什么,但即使每个按钮都有 onClick() 函数,这些按钮也不会链接到其他 HTML 文件。
我想知道是否应该在两个 HTML 文件的标题中添加某种链接标记。另外,我不太确定location.href 行在button.html 文件的脚本标记中的作用。我刚刚在网上找到了一些资源来尝试一下。
另外,我只需要使用 Vanila Javascript、HTML 和 CSS 来执行此操作。
请帮帮我。谢谢!!
【问题讨论】:
-
使用带有
href的n<a>标签而不是<button>,你根本不需要任何javascript -
@charlietfl 如果他喜欢按钮并且不想通过 css 制作按钮的外观
标签: javascript html css redirect