我使用twitter引導中的圖標。當用戶發佈狀態更新時,我希望其他用戶能夠喜歡,不喜歡或喜歡該帖子。在他們喜歡這篇文章之後,我想要改變圖形的顏色,以便他們知道他們已經按下了按鈕。從其他堆棧溢出帖子中,我找到了沒有jQuery的方法。但我想用jQuery來完成它。目前,當我使用$(this).toggleClass(tst)
添加我想要的顏色時,顏色不會改變。我知道我的ajax正在工作,因爲數據庫根據我點擊的特定帖子進行更新。
奇怪的是,如果我使用$(「#喜歡」)。toggleClass(tst),顏色確實會改變,但僅在使用#liked id的第一篇文章中有所變化。我試過使用toggleClass,addClass,removeClass/addClass .css,並且在函數內部和外部調用$(this).toggleClass(tst)。這一切都歸結於同一件事:當我使用this
它不會改變顏色。我如何(使用jQuery)來定位特定的喜歡按鈕,以便我可以改變它的類。
echo "<a id='liked' href='#' <span class='glyphicon glyphicon-thumbs-up'></span> </a>";
以下是Twitter的引導CSS的
.glyphicon-thumbs-up:before{
content:"\e125";
color: #7f8c8d;
opacity: 0.7;
}
.glyphicon-thumbs-up.tst:before{
content:"\e125";
color: green;
}
,這裏是我的jQuery
$(".glyphicon").click(function() {
var socialButton = $(this).attr('id');
//traverse the dom up until I get to this post's text. Get the text from the post
var thisPost = $(this).parent().parent().text();
//get the user name of the post
var user = $(this).parent().parent().parent().text();
$.ajax({
type: "POST",
dataType: "json",
url: "socialbuttons.php",
data: {
button: socialButton,
post: thisPost,
postAuthor: user
},
success: function(data) {
$(this).toggleClass("tst");
alert("success")//this works
//$("#liked").toggleClass("tst"); //works, but only on first post
}
})
})
我看到您的代碼生成了無效的HTML,即「
Can you be more specific? – jonjon
sir correct your HTML..It's nbot generating valid HTML A tag –