PHP 实现动态按钮跳转:从文本文件读取颜色和文字
<!DOCTYPE html>
<html>
<head>
<title>动态按钮跳转网页</title>
<style type="text/css">
.button {
display: block;
margin: 50px auto;
width: 80px;
height: 30px;
line-height: 30px;
text-align: center;
cursor: pointer;
}
</style>
</head>
<body>
<?php
// 读取文本文件内容
$file = fopen("a.txt", "r");
if ($file) {
while (($line = fgets($file)) !== false) {
// 解析每行内容
$parts = explode(" ", $line);
$name = trim($parts[0]);
$color = trim($parts[1]);
<pre><code> // 输出按钮
echo "<div class='button' style='background-color: $color;'>$name</div>";
}
fclose($file);
} else {
echo "无法打开文件 a.txt";
}
?>
<script type="text/javascript">
// 获取所有按钮
var buttons = document.querySelectorAll(".button");
// 为每个按钮添加点击事件
for (var i = 0; i < buttons.length; i++) {
var button = buttons[i];
button.addEventListener("click", function() {
// 根据按钮名称跳转到对应的URL
var name = this.innerHTML;
switch (name) {
case '按钮A':
window.location.href = "A.html";
break;
case '按钮B':
window.location.href = "B.html";
break;
case '按钮C':
window.location.href = "C.html";
break;
}
});
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/ovzB 著作权归作者所有。请勿转载和采集!