【发布时间】:2014-01-31 23:47:14
【问题描述】:
我正在通过此代码更新用户帖子:
var auto_refresh = setInterval(function()
{
$('.contenido-publicaciones')
.load("refresh_p.php?search_user="+search_user).fadeIn();
}, 2500 ); //Which is working.
1:<div class="contenido-publicaciones">,所有来自 refresh.p.php 的 <div class="this-is-a-post"> div 每 2.5 秒加载一次。
2:我在每个 <div class="this-is-a-post"> 内部都有一个 textarea 标签,从 refresh_p.php 刷新。
3:<div class="contenido-publicaciones">刷新时,也会刷新textarea,阻止用户输入。
4:所有文本区域都有b_green类
当用户单击类为b_green 的文本区域时,我想clearInterval(auto_refresh),以便其他用户可以向该帖子发送评论。
我尝试了以下方法:
$(".b_green").click(function(){
clearInterval(auto_refresh);
}); // 没用,<div class="contenido-publicaciones"> 继续刷新。
$("textarea").click(function(){
clearInterval(auto_refresh);
}); // 仅当我单击页面顶部的文本区域以发送帖子时有效,但当我单击具有类 b_green 的文本区域时不起作用。
$(this).click(function(){
clearInterval(auto_refresh);
}); //如果我点击 b_green textareas 就可以了,但是在用户每次点击页面时停止间隔,我只想在点击 b_green textareas 时停止。
我的代码有错误吗?任何帮助将不胜感激。
【问题讨论】:
-
var auto_refresh分配的范围是什么?它是窗口的本地还是全局 -
看起来您需要使用委托,因为您的
.b_green文本区域正在动态添加。使用.on 委托静态父级的事件。 -
Please read the Help so that you can format code properly next time you ask a question. 另外我认为您遗漏了一些重要的代码。设置间隔计时器的第一个代码块究竟在哪里?
标签: javascript php jquery html css