- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下适用于 AJAX、PHP、PDO、HTML 和 CSS 的代码。它的功能是评级按钮,通过向 MySQL 添加 +1 来发送到 AJAX 帖子上的数据库或从中取-1,只是一个普通的评级系统,如 Facebook 的“点赞”。
当前情况如下:一旦我按下(+0)按钮,它就会调用ajax并将其更新为(+1),在这种情况下,背景从正常的白色更改为蓝色背景颜色是由 AJAX 表单上的toggleClass 添加的。我遇到的问题是,刷新页面后,只有背景会保存,并且 plus 和rateCount 返回到它正常颜色。我想做的就是一旦按下当前处于 +0 且字体颜色为深色的按钮,我希望它切换为白色,并且一旦刷新页面我希望它留在那里。以下是我在一些 SO 用户的帮助下如何做到这一点的。
注意:$voterate 返回来自 MySQL 的点赞总数。
<小时/>.up { -moz-user-select: none; background-color: #FFFFFF; background-image: none; border: 1px solid #DDDDDD; border-radius: 3px 3px 3px 3px;box-shadow: 0 1px 0 rgba(0, 0, 0, 0.05); cursor: pointer; float: left; height: 28px; line-height: 26px; margin-left: 10px; outline: medium none; padding: 0 10px; transition: background-color 0.218s ease 0s, border-color 0.218s ease 0s, box-shadow 0.218s ease 0s; width: auto;}
.up:hover{ border-color: #BFBFBF; box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1);}
.up.clicked { background-color: #427FED;background-image:-moz-linear-gradient(center top , transparent, transparent); border: 1px solid transparent;}
.clicked:hover{ -moz-border-bottom-colors: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #4285F4; background-image: -moz-linear-gradient(center top , transparent, transparent); border-color: transparent transparent #2F69C2; border-image: none; border-style: solid; border-width: 1px; box-shadow: 0 -1px 0 #2F69C2 inset;}
.plus { color: #696969; font-family: Segoe UI; font-size: 16px; font-weight: bold;}
.plus.PlusWhiteButton { color:#fff;}
.rateCount { color: #696969; font-size: 15px; font-weight: bold;}
.rateCount.RateCountWhiteButton { color:#fff;}
<小时/>
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$sth = $db->prepare("SELECT add_iP FROM PostsRating WHERE post_iD_fk = :post_iD AND add_iP = :ip");
$sth->execute(array(':post_iD' => $post_iD, ':ip' => $ip));
$clicked = ($sth->fetchColumn()) ? " clicked" : ""; // i'm confuse on how this get's the clicked class, it seems really inneficient but it works one way or another.
?>
<span class="up vote<?php echo $clicked;?>" name="voteUp" id="<?php echo $post_iD;?>">
<span class="plus">+</a>
<span class="rateCount"><?php echo $VoteRate;?></a>
</span>
<小时/>
$(function()
{
$(".vote").click(function()
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
if (name=='voteUp')
{
$.ajax(
{
type: "POST",
url: "voting/up_vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.toggleClass("clicked"); // clicked function changes the background and is the only one that actually stays once the page is refreshed.
parent.find(".rateCount").html(html);
parent.find(".rateCount").toggleClass("RateCountWhiteButton");
parent.find(".plus").toggleClass("PlusWhiteButton");
}
});
}
return false;
});
});
以下是我如何从 AJAX 添加信息。不确定是否需要。
$ip = $_SERVER['REMOTE_ADDR'];
if($_POST['id'])
{
$sth = $db->prepare("SELECT add_iP FROM PostsRating WHERE post_iD_fk = :id AND add_iP = :ip");
$sth->execute(array(':id' => $_POST['id'], ':ip' => $ip));
if( $sth->fetchColumn() == 0)
{
$sth = $db->prepare("UPDATE posts set voteUp = voteUp+1 where post_iD = :id");
$sth->execute(array(':id' => $_POST['id']));
$sth = $db->prepare("INSERT into PostsRating (post_iD_fk, add_iP) VALUES (:id, :ip)");
$sth->execute(array(':id' => $_POST['id'], ':ip' => $ip));
} else {
$sth = $db->prepare("UPDATE posts set voteUp = voteUp-1 where post_iD = :id");
$sth->execute(array(':id' => $_POST['id']));
$sth = $db->prepare("DELETE FROM PostsRating WHERE post_iD_fk = :id AND add_iP = :ip");
$sth->execute(array(':id' => $_POST['id'], ':ip' => $ip));
}
$sth = $db->prepare("SELECT voteUp FROM posts WHERE post_iD = :id");
$sth->execute(array(':id' => $_POST['id']));
$row = $sth->fetch();
echo $row['voteUp'];
}
我不确定我是否解释清楚任何人都能理解,所以这里再次更清楚。
[+0](color:black;background:white;) 如果未单击,则处于待机状态。
如果我点击按钮,它会变成[+1](color:white;background:blue;)
如果我点击[+1]按钮删除我的评分,那么它会变成[+0](color :黑色;背景:白色;)。唯一的问题是,如果我点击它,它将保持白色和蓝色背景。
最佳答案
这会将类添加到 plus
和rateCount
跨度类似于 clicked
的方式类被添加到 up
跨度。
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$sth = $db->prepare("SELECT add_iP FROM PostsRating WHERE post_iD_fk = :post_iD AND add_iP = :ip");
$sth->execute(array(':post_iD' => $post_iD, ':ip' => $ip));
$clicked = $pluswhite = $ratewhite = '';
if ($sth->fetchColumn()) {
$clicked = " clicked";
$pluswhite = " PlusWhiteButton";
$ratewhite = " RateCountWhiteButton";
?>
<span class="up vote<?php echo $clicked;?>" name="voteUp" id="<?php echo $post_iD;?>">
<span class="plus<?php echo $pluswhite;?>">+</a>
<span class="rateCount<?php echo $ratewhite;?>"><?php echo $VoteRate;?></a>
</span>
关于php - 在 AJAX SUCCESS 上更改并保存 SPAN CSS,然后检索保存的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17460681/
问题:假设我们有一个名为success 的C++ 字符串。 &success 和 success& 有什么区别? 这题是我期中复习的,我理解&success是获取对象的地址。但我不确定 success
我正在阅读 gearman 代码的手册页( http://manpages.ubuntu.com/manpages/precise/man3/gearman_success.3.html )。他们有两
在 Angular 中,[class]="'success'" 和 class="success" 有区别吗? 例如: ... 或 ... 如果同时使用 IIUC 后者会导致替换前者,所以我想更好
当我查看日志文件时 D:\SAS\XXX\Lev1\SASMain\BatchServer\Logs 我看到了这两行 NOTE: Libref TESTLIB successfully assigne
我正在努力完成这项工作: $http.post('/route/path', {'username': $scope.threadedUsers[currentIndex].name}). s
我正在尝试设置协议(protocol)来处理我的 javascript 函数中的成功和错误,但是当函数成功时它似乎并不合作。我收到以下错误: Failed with: TypeError: Objec
在 typescript 中,我有一个 DataAccess 类,以便所有 Ajax 调用都通过单个对象进行路由,以节省应用程序中许多地方的代码重复。 在使用这种方法时,我需要使用回调将响应返回到调用
我已经用 Spring Security 3.0.2 实现了一个登录-注销系统,一切都很好,但是对于这一点:在我添加了一个带有 invalid-session-url 属性的 session 管理标签
运行命令pip install rpy2会导致以下输出和错误消息:。我按照另一篇Stackoverflow帖子上的说明卸载并重新安装了R和相关目录,但得到了相同的错误。我还尝试安装以前版本的rpy2,
我最近在项目的测试目标中添加了一个新的单元测试,当我点击产品 > 测试时,Xcode 说“测试成功”。我相当确定测试应该失败,因为被测试的方法尚未实现。我在测试中添加了一个断点,但从未到达过;测试仍然
我正在编写一个程序来创建一个AD帐户并启用Exchange邮箱,并且从中得到一些奇怪的行为。 首先,尽管它成功创建了AD用户,但由于“找不到MyPath/先生示例”,因此无法启用邮箱。我认为这是由于A
在cd_deployer_conf文件中我们设置了Cleanup="false,这样传输包在发布后不会被删除,但是我们可以看到单个发布操作有3个不同的包喜欢 tcm_0-264891-66560.CO
我正在使用 $().each() 循环访问一些项目。我想确保这段脚本之后的操作仅在 each() 完成时执行。 示例: $('something').each(function() { // do
我正在尝试创建一个 ping 洪水程序,它将目标 IP 地址和广播 IP 地址作为参数。该程序将向广播地址发送 icmp echo 数据包,并将受害者的 IP 地址作为源。网络上所有收到数据包的主机都
我正在实现一个虚拟 LTE EPC 设置,其模块 (HSS) 之一需要 mysql 数据库。创建后,我必须运行该模块,但收到屏幕截图 1 中所示的错误。附:我对这个东西很陌生 最佳答案 您必须修改您的
#include #include #include #include #include #include #include #include #include void error(cha
我的基本代码如下, fd = open("test.file", O_RDONLY); if (read(fd, &tempch, 1) < 1) { perror("F
更新 :这似乎是在 Pipeline: Declarative 中引入的错误插件版本 1.3.5 - 降级到 1.3.4.1解决了这个问题。票证创建于: https://issues.jenkins-
我最近在 jQuery 网站上看到了弃用通知。 Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete()
我假设调用 status.success() 会终止后台作业。但是,当我运行它时,我仍然看到第二条日志消息: Parse.Cloud.job("Tester", function (request,
我是一名优秀的程序员,十分优秀!