用Sublime Text 完成实验三 数据在不同页面中的传递。 1.第一个登录页面,里面有提交表单, action 提交到 index.html页面 2.第二个页面之所以可以使用第一个页面的数据,是利用了URL 里面的 location.search参数 3.在第二个页面中,需要把这个参数提取。 4.第一步去掉? 利用 substr 5.第二步 利用=号分割 键 和 值 split(‘=‘) 6.第一个数组就是键 第二个数组就是值 第一个页面 第二个页面,通过location.search获得传递过来的参数
时间: 2023-11-27 13:51:44 浏览: 31
好的,Sublime Text 是一个非常流行的代码编辑器,非常适合前端开发。实验三中的数据在不同页面中的传递可以通过 URL 参数来实现。具体步骤如下:
第一个登录页面中,我们需要设置一个表单,将表单提交到 index.html 页面:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="index.html" method="get">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
在第二个页面中,我们可以使用 URL 中的 location.search 参数来获取第一个页面中提交的表单数据。具体步骤如下:
```html
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<script>
// 获取 URL 中的参数
var params = location.search.substr(1).split('&');
var username = "";
var password = "";
for (var i = 0; i < params.length; i++) {
var pair = params[i].split('=');
if (pair[0] === 'username') {
username = decodeURIComponent(pair[1]);
}
if (pair[0] === 'password') {
password = decodeURIComponent(pair[1]);
}
}
// 使用参数渲染页面
document.write("<p>Username: " + username + "</p>");
document.write("<p>Password: " + password + "</p>");
</script>
</body>
</html>
```
在第二个页面中,我们首先获取 URL 中的参数,然后使用 substr 函数去掉参数中的问号,使用 split 函数将参数分割成键值对的数组。然后我们遍历这个数组,将键值对分别存储到 username 和 password 变量中。最后,我们可以使用这些参数来渲染页面。
希望这个回答对你有所帮助!