将按钮添加到Javascript倒计时

问题描述 投票:1回答:6

我对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);
<p id="demo" class="countdown-live" style="text-align:center;"></p>

任何帮助,以显示超链接按钮,而不是文本“我们在Facebook上生活!”我将不胜感激。

javascript html countdown
6个回答
3
投票

只需将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);
<p id="demo" class="countdown-live" style="text-align:center;"></p>

4
投票

在innerHTML()属性中,您可以传递一个HTML标记,如

<a href="...">
  <button> YOUR TEXT </button> 
</a>

1
投票

您可以创建一个HTML按钮元素,其中包含指向您尝试指向的页面的链接。确保您为该按钮指定了“演示”ID,以便它与您当前的代码一起使用

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

1
投票

你需要创建一个这样的按钮:

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

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

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

1
投票

你可以试试这个:

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";

我希望这对你有帮助。


0
投票

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

<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!";
  }
© www.soinside.com 2019 - 2024. All rights reserved.