我的网站是gapagap.com,当我向下滚动帖子时,它会加载更多帖子,但是其中有一个功能“投票”,其中点击有时适用于某些帖子,有时不适用于随机帖子。 这是代码片段..
的index.php
<div id="entries-content" class="list">
<ul id="entries-content-ul" class="col-1">
{section name=i loop=$posts}
{include file="posts_bit.tpl"}
{/section}
{literal}
<script type="text/javascript">
$(document).ready(function(){
//calling header function to set the active tab state
headerTabChange('hot');
var tpage = 2;
function lastAddedLiveFunc()
{
$('div#lastPostsLoader').html('');
$.get("{/literal}{$baseurl}/{literal}indexmore.php?page="+tpage, function(data){
if (data != "") {
$(".col-1").append(data);
}
$('div#lastPostsLoader').empty();
});
};
$(window).scroll(function(){
var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
var scrolltrigger = 0.95;
if ((wintop/(docheight-winheight)) > scrolltrigger) {
lastAddedLiveFunc();
tpage = tpage+1;
$('#backtotop').show();
}
});
});
</script>
{/literal}
</ul>
<div id="lastPostsLoader"></div>
</div>
</div>
{include file='right.tpl'}
{include file='vote_js.tpl'}
这里是vote_js.tpl的代码:
{literal}
<script type="text/javascript">
$('.vote').click(function(){
if( $(this).hasClass('loved')){
$(this).removeClass('loved');
likedeg(-1,$(this).attr('rel'));
}else{
likedeg(1,$(this).attr('rel'));
$(this).addClass('loved');
}
});
function likedeg(x,p){
jQuery.ajax({
type:'POST',
url:'{/literal}{$baseurl}{literal}'+ '/likedeg.php',
data:'art='+x+'&pid=' + p,
success:function(e){
$('#love_count_'+p).html(e);
}
});
}
</script>
{/literal}
这是另一个在 post.tpl
中具有投票功能的代码 <li>
{if $smarty.session.USERID ne ""}
<a class="vote love {insert name=get_fav_class value=var PID=$posts[i].PID}" id="post_love_{$posts[i].PID}" rel="{$posts[i].PID}" href="javascript:void(0);"><span>{$lang144}</span></a>
{else}
<a class="vote love " id="post_love_{$posts[i].PID}" rel="{$posts[i].PID}" href="{$baseurl}/login"><span>{$lang144}</span></a>
{/if}
</li>
投票功能在function.php
内 function insert_get_fav_status($var)
{
global $conn;
$query="SELECT count(*) as total FROM posts_favorited WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."' AND PID='".intval($var[PID])."'";
$executequery=$conn->execute($query);
$total = $executequery->fields[total];
//echo $total.'<br />';
//var_dump($var);
return intval($total);
}
function insert_get_fav_class($var)
{
global $conn;
$query="SELECT count(*) as total FROM posts_favorited WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."' AND PID='".intval($var[PID])."'";
$executequery=$conn->execute($query);
$total = $executequery->fields[total];
//echo $total.'<br />';
//var_dump($var);
if (intval($total) == 1) return("loved");
else return ("");
}
function insert_get_unfav_status($var)
{
global $conn;
$query="SELECT count(*) as total FROM posts_unfavorited WHERE USERID='".mysql_real_escape_string($_SESSION[USERID])."' AND PID='".intval($var[PID])."'";
$executequery=$conn->execute($query);
$total = $executequery->fields[total];
return intval($total);
}
function insert_get_fav_count($var)
{
global $conn;
$query="SELECT count(*) as total FROM posts_favorited WHERE PID='".intval($var[PID])."'";
$executequery=$conn->execute($query);
$total = $executequery->fields[total];
return intval($total);
}
请理解我是一名PHP开发人员。我在JAVASCRIPT和JQUERY中知道零知识。
答案 0 :(得分:2)
$.get("{/literal}{$baseurl}/{literal}indexmore.php?page="+tpage).done(function(data) {
if (data != "") {
$(".col-1").append(data);
}
$('div#lastPostsLoader').empty();
});
试试这个
答案 1 :(得分:2)
很抱歉再次回答我自己的问题.....但坦率地说,我不想改变这种习惯......很多。
代替
{ $(".col-1").append(data);
我将其替换为{ $(".col-1").html(html()+data);
感谢大家的支持。
答案 2 :(得分:1)
如果您的意思是这段代码:
<script type="text/javascript">
(function (d, buildThese) {
var homeScript, newScript, n = buildThese.length, i;
for (i = 0; i < n; i = i + 1) {
newScript = d.createElement('SCRIPT');
newScript.type = 'text/javascript';
newScript.async = true;
newScript.src = buildThese[i];
homeScript = d.getElementsByTagName('SCRIPT')[0];
homeScript.parentNode.insertBefore(newScript, homeScript);
}
});
</script>
你确实意识到你正在调用这个闭包:
两个粗体原因是导致代码搞砸的原因。如果你没有任何东西,buildThese将是未定义的,其长度将为0.因此,循环将永远不会运行。即使它确实如此,d也是未定义的,因此你会在d.createElement上遇到致命的错误。
你想要的功能是jQuery done()。这个聪明的小函数允许您设置一个回调,该回调将在插入所有新元素时触发。它的工作原理如下:
$.get("myURL").done(function() { alert("I'm done!"); });
这也会对您的代码产生两种意外影响: