【问题标题】:Adding a Button to a Javascript Countdown将按钮添加到 Javascript 倒计时
【发布时间】:2019-03-19 00:07:52
【问题描述】:

我对 Javascript 非常陌生,并且不太了解它。但是,我已经设法创建了一个倒数计时器,它可以像我想要的那样半工作。它非常简单,但它基本上是一个倒计时到特定日期的计时器,然后一旦达到指定的日期和时间,它就会显示我可以自定义的文本。 一旦倒计时达到零,我希望这段代码能够显示带有超链接的按钮。这是我到目前为止的代码:

// Set the date we're counting down to
var countDownDate = new Date(Date.now() + 20000).getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = days + " days, " + hours + " hours, " +
    minutes + " minutes, & " + seconds + " seconds";

  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "We're Live on Facebook!";
  }
}, 1000);
&lt;p id="demo" class="countdown-live" style="text-align:center;"&gt;&lt;/p&gt;

任何帮助让它显示一个超链接按钮而不是文本“我们在 Facebook 上直播!”将不胜感激。

【问题讨论】:

  • 所以更改显示文本的行。 document.getElementById("demo").innerHTML = "We're Live on Facebook!";我没有看到你试图解决这个问题。

标签: javascript html countdown


【解决方案1】:

在innerHTML() property中你可以传递一个像

这样的HTML标签
<a href="...">
  <button> YOUR TEXT </button> 
</a>

【讨论】:

  • innerHTML 不是函数。
  • 没错,它是一个属性。我的坏;)
【解决方案2】:

只需将 HTML 添加到您正在设置的字符串中:

// Set the date we're counting down to
var countDownDate = new Date(Date.now() + 20000).getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = days + " days, " + hours + " hours, " +
    minutes + " minutes, & " + seconds + " seconds";

  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = '<a href="https://facebook.com">We\'re Live on Facebook!</a>';
  }
}, 1000);
&lt;p id="demo" class="countdown-live" style="text-align:center;"&gt;&lt;/p&gt;

【讨论】:

  • 效果很好。有点生自己的气,就这么简单,哈哈。谢谢!
【解决方案3】:

您可以只创建一个 HTML 按钮元素,其中包含指向您尝试指向的页面的链接。确保为按钮提供“演示”的 ID,以便它适用于您当前的代码

<button id="demo" onclick="window.location.href = 'https://www.facebook.com/';">We're live on Facebook!</button>

【讨论】:

    【解决方案4】:

    您需要像这样创建一个按钮:

    <button id="myButton" style="display:none"><a href="example.com">Button Text</a></button>
    

    然后你可以像这样通过 JavaScript 显示它:

    document.getElementById("myButton").style.display = "inline";
    

    【讨论】:

      【解决方案5】:

      你可以试试这个:

      document.getElementById("demo").innerHTML = "<a href='https://www.youUrl.com'>We're Live on Facebook!</a>";
      

      使用 innerHTML 属性,您可以添加原始 HTML。您还可以添加隐藏标签,然后使其可见:

      对于可见:

      document.getElementById("yourID").style.visibility = "visible";
      

      不可见:

      document.getElementById("main").style.visibility = "hidden";
      

      希望对你有所帮助。

      【讨论】:

        【解决方案6】:

        p 之后的 html 中添加一个按钮并添加一个类来初始隐藏它

        <p id="demo" class="countdown-live" style="text-align:center;"></p>
        <button id='liveButton' type='button' class='hide'>Your Text</button>
        

        在css中添加类hide

        .hide{
         display:none;
        }
        

        在 js 中当我达到 0 时删除该类

        if (distance < 0) {
            clearInterval(x);
            document.getElementById("liveButton").classList.remove('hide')
            document.getElementById("demo").innerHTML = "We're Live on Facebook!";
          }
        

        【讨论】:

          猜你喜欢
          • 2023-04-11
          • 1970-01-01
          • 2015-06-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多