Javascript Ajax 404(未找到)在循环中循环
我尽量不重复使用相同的代码,所以我想我应该使用jQueryJavascript Ajax 404(未找到)在循环中循环,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我尽量不重复使用相同的代码,所以我想我应该使用jQuery.each()方法。每次重新加载网页时,都会出现以下ajax错误: 循环不断地把它变成一个对象。这是我的密码: var counterColors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'gray', 'brown'], $currentCounterColor; $(counterColors).each(function() {
.each()
方法。每次重新加载网页时,都会出现以下ajax错误:
循环不断地把它变成一个对象。这是我的密码:
var counterColors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'gray', 'brown'], $currentCounterColor;
$(counterColors).each(function() {
$currentCounterColor = $(this);
(function($) {
$(document).ready(function() {
$.ajaxSetup({
cache: false,
success: function() {
$('#count_' + $currentCounterColor).fadeIn(300);
}
});
$currentCounterColor.load('./' + $currentCounterColor + '.php');
var refreshId = setInterval(function() {
$currentCounterColor.load('./' + $currentCounterColor + '.php');
}, 60000);
})
})(jQuery);
});
我有9个这样的ID:
#count_red
#count_orange
#count_yellow
... etc
red.php
orange.php
yellow.php
然后我有9个PHP文件,如下所示:
#count_red
#count_orange
#count_yellow
... etc
red.php
orange.php
yellow.php
我不熟悉这个错误。我想你应该像贝娄一样试试。因为您没有将任何参数带到
每个
并使用此
引用此
将返回一个字符串,如{0:r',1“e”,2:“d”}
因此,当您连接时,它将返回#count\u[object object]
而不是#count\u red
我只是告诉你怎么做
var counterColors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'gray', 'brown'], $currentCounterColor;
$(counterColors).each(function(index,value) {
$currentCounterColor = value;
$('#count_' + $currentCounterColor).fadeIn(300); // here first time it will call the #count_red.load()
});
但我不知道你想说什么
$currentCounterColor.load('./' + $currentCounterColor + '.php');
因此,只需查看一下过程,并相应地更改您的实现。您的
每个anon函数都没有任何参数,那么您如何知道您使用的是哪一项呢?那么我是否将数组作为参数?$(此)
很可能不是您认为的……您不应该在这里使用它(这是为了在DOM节点上循环),但是(并且将jQuery
作为$
传递给函数似乎也没有必要)最终完成了;但是,v
不是全局的,所以idk我如何加载()
我一直在玩它,确实如此。你的想法是什么?说$currentCounterColor
没有定义!“未定义不是函数”@Matthew如果没有任何代码,我怎么知道,添加一个演示让我知道。