【问题标题】:jQuery script works on local maschine, but not on web hostjQuery 脚本适用于本地机器,但不适用于 Web 主机
【发布时间】:2015-12-30 15:04:42
【问题描述】:

我正在使用 The Final Countdown v2.0.4 ,它在我的本地机器上工作得很好,但在我的网络主机上,它似乎没有按预期工作。它没有倒计时,而是立即转到 00:00。

jQuery 代码:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery.countdown.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function()    {
            $(".timeLeft").each(function(i, obj) {
                var $dis = $(this), finalDate = $(this).text().replace("-", "/").replace("-", "/");
                var u = 0;
                $dis.countdown(finalDate, function(event) {
                    $dis.text(event.strftime('%M:%S'));
                    if(event.offset.minutes < 25 && u == 0) {
                        $dis.next().next().find("input[type='image']").attr("src", "images/glyphicons/png/glyphicons-207-ok.png");
                        u++;
                        $dis.next().next().find("input[type='image']").attr("title", "Acceptera uppdraget");
                    }
                });
            });
        });
    </script>

具体的HTML代码:

<thead class="mission_title">
    <tr>
        <td class="timeLeft"><?php echo /*$interval->format("%I:%S")*/ $missions[$i]["expire"]; ?></td>
        <td><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?do=request&id=" . $missions[$i]["id"]; ?>"><input type="number" title="Föreslå ersättning" style="width:35px;" min="<?php echo $missions[$i]["payment"]; ?>" value="<?php echo $missions[$i]["payment"]; ?>" name="price_offer"> <input name="request" type="image" src="<?php echo ($_SESSION["plus"] == 1 ? "images/glyphicons/png/glyphicons-207-ok.png" :  "images/glyphicons/png/glyphicons-208-remove.png"); ?>" alt="Acceptera Uppdraget" title="<?php echo ($_SESSION["plus"] == 1 ? "Acceptera uppdraget." : "Du måste vänta fem minuter innan du kan acceptera uppdraget." );?>" value="send"></td>
    </tr>
</thead>

感谢任何帮助,我尝试搜索并且许多人建议区分大小写可能是一个问题;我无法在控制台或校对代码中找到任何内容,所以我不知道问题出在哪里。 :(

编辑:

jQuery 导入工作正常,我一直在使用其他 jQuery 函数,例如 .click(),并且没有问题。

【问题讨论】:

  • 这似乎很明显,但是 jquery.countdown.min.js 在服务器上的正确位置吗?
  • 是的!该函数运行但它没有倒计时它立即进入 00:00

标签: javascript jquery localhost


【解决方案1】:

将类名从“timeLeft”更改为“time-left”。

这是一种有点不同的方法,但它可能会让你的事情变得更容易。

让您的 php 脚本将目标时间作为数据属性输出(在此处使用纪元时间;秒或毫秒都可以。我假设是秒。):

<td class="time-left" data-countdown-expire=
<?php echo $missions[$i]["expire"]; ?>
> <!-- rest of td goes here ... -->

然后在 Javascript 中,我使用矩库进行任何类型的时间操作。很容易让它看起来像你想要的那样。

这是一个每秒更新显示的示例:

setInterval(
function(){ 
var timerNodes = document.getElementsByClassName('time-left');
  var timers = Array.prototype.slice.call(timerNodes)
timers.map(function (el) {
 var endTime = el.getAttribute('data-countdown-expire');
 var timeUntil = moment.unix(endTime).fromNow();
 el.innerHTML = timeUntil;
})
}, 1000);

Codepen example here

【讨论】:

  • 谢谢,我使用 PHP 中的 strtotime,然后使用 javascript 将其转换为本地时间,它在本地机器和主机服务器上显示相同的结果。
【解决方案2】:

我很笨,

我从没想过 jQuery 会使用本地时区;所以我必须将 PHP 输出转换为用户的时区,因为脚本不允许我为转换设置时区.. x_x

【讨论】:

  • 您应该考虑将时间转换为纪元时间,然后使用javascript转换为本地时间。您的服务器永远无法确定客户端所在的时区,而 javascript 可以轻松地将纪元时间转换为本地时间。
猜你喜欢
  • 2016-01-14
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
相关资源
最近更新 更多