小程序 创建一个选择设备html+js
时间: 2023-09-18 22:06:52 浏览: 42
好的,以下是一个简单的HTML和JavaScript代码示例,它可以创建一个选择设备的页面,包含三个设备选项(手机、电脑和平板),并且可以根据用户的选择执行相应的操作。
```html
<!DOCTYPE html>
<html>
<head>
<title>选择设备</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
h1 {
text-align: center;
margin-top: 50px;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 50px;
}
.btn {
background-color: #4CAF50;
color: white;
padding: 15px;
border: none;
border-radius: 5px;
width: 200px;
text-align: center;
text-decoration: none;
font-size: 20px;
margin-top: 20px;
cursor: pointer;
}
.btn:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>请选择您要连接的设备:</h1>
<div class="container">
<button class="btn" onclick="connectDevice('phone')">手机</button>
<button class="btn" onclick="connectDevice('computer')">电脑</button>
<button class="btn" onclick="connectDevice('tablet')">平板</button>
</div>
<script>
function connectDevice(device) {
switch (device) {
case "phone":
console.log("正在连接手机...");
window.location.href = "https://www.example.com/phone";
break;
case "computer":
console.log("正在连接电脑...");
window.location.href = "https://www.example.com/computer";
break;
case "tablet":
console.log("正在连接平板...");
window.location.href = "https://www.example.com/tablet";
break;
default:
console.log("无效的设备选项。");
}
}
</script>
</body>
</html>
```
这个页面包含一个标题和一个包含三个设备选项的容器。每个选项都是用一个带有onclick事件的按钮表示的。当用户单击任何一个按钮时,它会调用JavaScript函数connectDevice,并将设备名称作为参数传递进去。然后,根据设备名称,函数会调用不同的操作。在此示例中,函数会使用window.location.href属性打开一个指定的URL,以模拟连接到设备。如果设备名称无效,函数会将错误信息打印到控制台。您可以根据需要修改URL,以连接到不同的设备或执行其他操作。