У меня есть проблема с этим JQuery кодом:JQuery код не работает в страницах, но работает в консоли
$("#shout-msg").val('').attr('disabled', false);
$("#shout-send").attr('disabled', false);
Он работает на консоли Google Chrome, но он не работает на файл JavaScript. Вот весь код.
var shoutbox = {
opened: false,
last: false,
interval: 0,
open: function() {
$("#shoutbox #open").hide('blind');
$("#shoutbox #main").show('blind');
shoutbox.opened = true;
$.cookie('shoutbox', 'aberta', { expires: 30, path: '/' });
shoutbox.update(1);
},
close: function() {
$("#shoutbox #main").hide('blind');
$("#shoutbox #open").show('blind');
shoutbox.opened = false;
$.cookie('shoutbox', 'fechada', { expires: 30, path: '/' });
},
send: function() {
var msg = $("#shout-msg").val();
if(shoutbox.opened == true && msg != '') {
$("#shout-msg, #shout-send").attr('disabled', true).removeClass('red');
$.ajax({
type: 'POST',
data: {'msg': msg},
dataType:"JSON",
url: '/lib/shout_enviar.php',
success: function(html) {
if(html.concluido == true) {
$("#shout-msg").val('').attr('disabled', false);
$("#shout-send").attr('disabled', false);
}
clearTimeout(shoutbox.interval);
shoutbox.update(1);
}
});
}
},
update: function(a) {
if(shoutbox.opened == true) {
if(a == 0) { shoutbox.last = 0; }
$.ajax({
type:'GET',
dataType:'JSON',
url: '/lib/shout_update.php?last='+shoutbox.last,
success: function(html) {
i = 0;
while(i < html.length) {
var dados = html[i];
if(dados['id'] > shoutbox.last) {
shoutbox.add(dados['id'], dados['nick'], dados['msg']);
shoutbox.last = dados['id'];
}
i++;
}
shoutbox.interval = setTimeout(function() {shoutbox.update(1);}, 2500);
}
});
}
},
add: function(id, nick, msg) {
var msg_add = '';
msg_add += '<div id="msg" class="msg-shout" rel="'+id+'"><div id="info"><div style="float:left;width:165px;">';
msg_add += '<b>'+nick+' enviou:</b><br />';
msg_add += msg + '</div>';
msg_add += '<div id="img" style="background:url(https://www.habbo.com.br/habbo-imaging/avatarimage?img_format=gif&user='+nick+'&action=std&direction=4&head_direction=4&gesture=sml&size=s);"></div>';
msg_add += '<div id="clear"></div></div></div>';
$("#shoutbox #messages #scroll").prepend(msg_add);
},
start: function() {
if($.cookie('shoutbox') == 'aberta') {
shoutbox.opened = true;
}
shoutbox.last = $("#shoutbox #msg:first-child").attr('rel');
shoutbox.update(0);
},
}
Извините, если мой английский плохо, я из Бразилии.
Спасибо!
@dystroy #scroll находится внутри # сообщений, которые находятся внутри #shoutbox –
Используйте '$ (function() {/ * Код здесь * /})'. – gdoron
@FNXHenry Если я смотрю на функцию добавления, она добавляет каждый элемент времени к тому же идентификатору. –