JQuery代码不会优异

时间:2017-07-04 19:17:37

标签: jquery html

我正在进行重定向页面,当我测试它时,我看到100%的JQuery代码无法运行...所以请任何人帮我修复它?

var redirect_count = 5;

setTimeout(UpdateCounter, 1000);

function UpdateCounter() {
  if (redirect_count >= 1) {
    redirect_count = redirect_count - 1; // Another Method: redirect_count--;
    document.getElementById("counter").html("Redirecting after <strong>" + redirect_count + " seconds</strong>");
    document.title = "Redirecting after " + redirect_count + "sec - please wait...";
  } else if (redirect_count < 1) {
    window.location.assign("http://example.com");
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>[INFO]: This page has been moved to <a href="http://example.com">http://example.com</a>.</p>
<p id="counter">Redirecting after <strong>5 seconds</strong></p>

1 个答案:

答案 0 :(得分:0)

您需要将整个代码更改为实际使用 jQuery。

$("#counter").html(); // This is the right syntax!

单独包含jQuery库不会让你的页面很棒,但你需要使用jQuery库才能使它工作!

完整代码段

var redirect_count = 5;

setInterval(UpdateCounter, 1000);

function UpdateCounter() {
  if (redirect_count >= 1) {
    redirect_count = redirect_count - 1; // Another Method: redirect_count--;
    $("#counter").html("Redirecting after <strong>" + redirect_count + " seconds</strong>");
    document.title = "Redirecting after " + redirect_count + "sec - please wait...";
  } else if (redirect_count < 1) {
    window.location.assign("http://example.com");
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>[INFO]: This page has been moved to <a href="http://example.com">http://example.com</a>.</p>
<p id="counter">Redirecting after <strong>5 seconds</strong></p>

此外,您需要setInterval而不是setTimeout