我有其布局是很简单的一个页面的jQuery切换股利。所有文章(在foreach循环中)都包含一个类似.like(类似按钮)的按钮。当按下时,它很喜欢。现在让我们假设这是永久性的。如果VAR等于值不起作用
我有一个可变likestatus跟踪被按压倍#show_likes的数量。当像状态除以2没有剩余时,这意味着应该隐藏所有喜欢的帖子。如果有余额,所有喜欢的帖子应该是可见的。相当直接向前?
在页面加载,likestatus设置为1,自1 2 divded有剩余,都喜欢帖子被隐藏在页面加载。这适用于Firefox和Chrome。
因为likestatus设置为1,用户决定喜欢的所有帖子应自动切换。这工作在Firefox但不是铬。
问题是,为什么?
的JavaScript
$(document).ready(function() {
likestatus = 1; //on pageload, likestatus is 1 so all liked posts are hidden.
$(document).on("click", ".like", function(){ //when like button is pressed do this
postID = $(this).attr('id').replace('like_', ''); // get the ID of the post
// Declare variables
value = '1'; //this represents that the post is liked to be stored in a database
myajax(); //send to database
return false;
});
function myajax(){ // Send values to database
$.ajax({
url: 'check.php', //check.php receives the values sent to it and stores them in the database
type: 'POST',
data: 'postID=' + postID + '&value=' + value, //send the post ID and like value
success: function(result) {
if (result.indexOf("No") < 0){ //If return doesn't contain string "No", do this
if (value == 1){ //If post is liked, do this
$('#post-' + postID).removeClass('dislike').addClass('like'); //sets div class of the post to liked
$('#likebtn_' + postID).removeClass('likeimgoff').addClass('likeimgon'); //changes the image of the like button so it is visibly activated
// UP TO HERE, THE CODE WOKS IN BOTH CHROME AND FIREFOX. IN CHROME, THE CODE BELOW DOESN'T WORK
// If Hide Liked button is on, toggle the post
if (likestatus % 2 == 0) {
} else {
$('#post-' + postID).toggle();
}
}
}
}
});
}
// THE CODE BELOW WORKS IN BOTH CHROME AND FIREFOX
$('#show_likes').on('click', function() { //When Hide Liked checkbox clicked, toggle all liked posts.
likestatus++; //increment likestatus
if (likestatus % 2 == 0) {
$('#hidelikedbtn').removeClass('hidelikedimgoff').addClass('hidelikedimgon'); // changes the image of the hide all liked button so it is visibly deactivated
} else {
$('#hidelikedbtn').removeClass('hidelikedimgon').addClass('hidelikedimgoff'); // changes the image of the hide all liked button so it is visibly activated
}
return false;
});
的index.php
<?php global $post; ?>
<div id="show_likes">
<a id="hidelikedbtn" class="hidelikedimgoff mstrctrlL" href="#"><span></span> </a>
</div>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post);
$msg_id= $post->ID;
?>
<div id="post-<?php the_ID(); ?>" class="post <?php post_class(); ?>">
<div id="post-<?php the_ID(); ?>-inside" class="inside">
<h2 class="posttitle">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent Link to', 'buddypress') ?> <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<div id="like_<?php the_ID(); ?>" class="like">
<a id="likebtn_<?php the_ID(); ?>" class="likeimgoff" href="#"><span></span></a>
</div>
<div class="entry">
<?php the_content(__('Read the rest of this entry →', 'buddypress')); ?>
</div>
</div> <!-- post-ID-inside -->
</div> <!-- post-ID -->
当我改变$( '#后' +帖子ID).toggle()为$( '#后' +帖子ID)的CSS( “可见性”, “隐藏”);它的作品(尽管这个帖子只是看不见而已)。重点是,代码没有工作,一路下跌至这条线和没有帖子ID得到认可,但一些关于Chrome的就是不让切换功能..
帖子ID设置完美的罚款。就像我说的,AJAX发送没有问题,确认消息甚至显示发送的postID。 Firefox报告该值是有效的,并且在我的标记中它是无效且有效的属性。我已经用成功函数中更多的代码更新了我的代码,以告诉你为什么我知道它是有效的......一些类被切换并且它们确实有效(类改变了按钮的图像。与所述反复.. – Sweepster 2012-03-24 02:23:38
我发现我的代码不工作在Firefox,但无法在Chrome。为什么呢? – Sweepster 2012-03-24 02:44:19
你可以添加一些样本标记你的问题的任何想法(如结构'“#后” + postID'? – pete 2012-03-24 03:11:36