You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

教会网站前端开发:实现随机显示可点击小链接按钮及问题修复

问题修复方案

你遇到了三个核心问题:随机显示/隐藏的JavaScript代码失效导致按钮直接消失、CSS关键帧动画没效果、按钮无法实现链接跳转。以下是针对性的修复步骤和完整可运行代码:

1. JavaScript代码错误修复

你写的JS里犯了几个基础语法错误:

  • 用了HTML的<!-- -->注释,这在JS里是无效的,会直接导致代码报错,要换成JS的//单行注释或/* */多行注释
  • 函数末尾多写了一个},导致函数语法不完整
  • 直接用button变量可能找不到元素,最好通过document.getElementById('button')明确获取
  • 页面加载后没有初始设置按钮可见,大概率一开始就隐藏了

修正后的JS代码:

function buttonRandom(){      
  // 获取按钮元素
  const button = document.getElementById('button');
  // 页面加载时先让按钮显示
  button.style.visibility = "visible";
  // 每3秒随机切换一次可见状态
  setInterval(() => {
    // 50%概率切换状态
    if (Math.random() >= 0.5) {
      button.style.visibility = "hidden";
    } else {
      button.style.visibility = "visible"; 
    }
  }, 3000); // 3000毫秒 = 3秒
}

2. CSS关键帧动画修复

你的@keyframes用了小括号()包裹关键帧内容,这是错误的语法,必须用大括号{},而且每个关键帧里的样式也要用大括号包起来。另外原CSS最后多了一个冗余的},也要删掉,不然会影响样式解析。

修正后的关键帧代码:

@keyframes offsetFix {
  0% { background-color: #ffffff; }
  90% { background-color: #ffffff; }
  91% { background-color: #fefffe; }
  100% { background-color: #eeeeee; }
}

3. 按钮链接功能实现

原来的按钮是<div>标签,本身不具备跳转功能,要改成<a>标签,同时保留原有的类名、ID和样式,再加上href属性指定你要跳转的链接地址就行。

示例代码:

<a class="button" 
   id="button" 
   style="background-color:black"
   href="你的目标链接地址">
  Jesus was probably born on The Feast of Tabernacles / the Feast of Booths / Sukkot
</a>

完整内嵌式可运行代码

下面是把所有代码整合到HTML里的版本,不需要外部CSS或JS文件,直接打开就能用:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    @keyframes offsetFix {
      0% { background-color: #ffffff; }
      90% { background-color: #ffffff; }
      91% { background-color: #fefffe; }
      100% { background-color: #eeeeee; }
    }
    body {
      background-color: #ffffff;
      animation: offsetFix 2s linear infinite;
    }
    .button, a:link, a:visited {
      border: none;
      color: #960018;
      padding: 0px 0px;
      width: 3px;
      height: 3px;
      text-align: center;
      text-decoration: none;
      display: block;
      position:fixed;
      top:316px;
      left:257px;
      z-index:7777777;
      font-size: 0px;
      border-radius: 0px;
      transition-duration: 0.01s;
      cursor: crosshair;
    }
    .button:hover, a:hover {
      background-color: #000000;
      color: #000000;
      width:9px;
      height:9px;
      transform: translateY(-7px) translateX(-7px);
    }
    .button:active, a:active {
      background-color: #960018;
      color: #ffffff;
      width:191px;
      height:27px;
      font-size: 11px;
      transform: translateY(-7px) translateX(-77%);
    }
  </style>
  <title>Browser</title>
  <script>
    function buttonRandom(){      
      const button = document.getElementById('button');
      button.style.visibility = "visible";
      setInterval(() => {
        if (Math.random() >= 0.5) {
          button.style.visibility = "hidden";
        } else {
          button.style.visibility = "visible"; 
        }
      }, 3000);
    }
  </script>
</head>
<body onload="buttonRandom()">
  <a class="button" 
     id="button" 
     style="background-color:black"
     href="你的目标链接地址">
    Jesus was probably born on The Feast of Tabernacles / the Feast of Booths / Sukkot
  </a>
  <h1>header</h1>
  <p>paragraph</p>
</body>
</html>

内容的提问来源于stack exchange,提问作者CryptoMynd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:44:58