jQuery 脚本可以在本地机器上运行,但不能在网络主机上运行
jQuery script works on local maschine, but not on web host
我正在使用 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 会使用本地时区;所以我必须将 PHP 输出转换为用户的时区,因为脚本不允许我设置转换时区.. x_x
将 class 名称从 "timeLeft" 更改为 "time-left"。
这是一种有点不同的方法,但它可能会让你的事情变得更容易。
让你的 php 脚本输出目标时间作为数据属性(这里使用纪元时间;秒或毫秒都可以。我假设是秒。):
<td class="time-left" data-countdown-expire=
<?php echo $missions[$i]["expire"]; ?>
> <!-- rest of td goes here ... -->
然后在 Javascript 中,我使用 moment 库进行任何类型的时间操作。很容易让它看起来像你想要的那样。
这是一个每秒更新您的显示的示例:
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);
我正在使用 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 会使用本地时区;所以我必须将 PHP 输出转换为用户的时区,因为脚本不允许我设置转换时区.. x_x
将 class 名称从 "timeLeft" 更改为 "time-left"。
这是一种有点不同的方法,但它可能会让你的事情变得更容易。
让你的 php 脚本输出目标时间作为数据属性(这里使用纪元时间;秒或毫秒都可以。我假设是秒。):
<td class="time-left" data-countdown-expire=
<?php echo $missions[$i]["expire"]; ?>
> <!-- rest of td goes here ... -->
然后在 Javascript 中,我使用 moment 库进行任何类型的时间操作。很容易让它看起来像你想要的那样。
这是一个每秒更新您的显示的示例:
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);