Javascript 如何针对特定类将视图切换到用户?
当用户单击“我的评论”按钮时,我想向他们显示该帖子的评论。到目前为止,在页面加载时,这些注释由类隐藏 我不知道如何隐藏评论,当用户点击一个特定的评论按钮时,只显示一篇文章的评论 现在我显示每个帖子的所有评论 我有一个post.\u id,我可以用来针对一组特定的评论,但我不确定如何使用该数据。有什么想法吗 玉锉Javascript 如何针对特定类将视图切换到用户?,javascript,jquery,Javascript,Jquery,当用户单击“我的评论”按钮时,我想向他们显示该帖子的评论。到目前为止,在页面加载时,这些注释由类隐藏 我不知道如何隐藏评论,当用户点击一个特定的评论按钮时,只显示一篇文章的评论 现在我显示每个帖子的所有评论 我有一个post.\u id,我可以用来针对一组特定的评论,但我不确定如何使用该数据。有什么想法吗 玉锉 .fullPostContainer each post in posts .media.white.paddingBoxForP
.fullPostContainer
each post in posts
.media.white.paddingBoxForPost
.media-left.media-middle
a.removeTextDeco(href=post.link, target='_blank')
img.media-object(src=post.img, alt='facebook logo')
.media-body
a.removeTextDeco(href=post.link, target='_blank')
div#mainTitlePost.media-heading.postTitleFont= post.title
div#mainShortDesc.descriptionFont= post.description
//form.formSub
// label(for='upvote')
button.upvoteClick.btn.btn-lg.btnColor(value= post._id)
span.glyphicon.glyphicon-fire= post.upvote
button.btn.commentsInit.btn-lg.btnColor(value= post._id)
span.glyphicon.glyphicon-comment
.commentsForTheWin.hideOnLoad
p this is some text
span
JS文件
$(function () {
$('.commentsInit').click(function () {
// grabs value of button which is post._id
// var uniCommID = $(this).attr("value");
$('.hideOnLoad').toggle("slow");
$.ajax({
type: "GET",
url: "/api/comments"
}).success(function (users) {
var a = JSON.stringify(users);
$('.commentsForTheWin span').text("Comments but not really for " + a);
})
})
});
目前,我正从数据库中得到我想要的东西,但我最难弄清楚如何针对特定的类(或者如果可能的话) 我使用jQuery
.closest()
从.commentsInit
按钮向上遍历树,在变量中存储对父级.paddingBoxForPost
的引用,并使用该引用指向相关元素
$(function() {
$('.commentsInit').click(function() {
// grabs value of button which is post._id
// var uniCommID = $(this).attr("value");
var $post = $(this).closest('.paddingBoxForPost');
$post.find('.hideOnLoad').toggle("slow");
$.ajax({
type: "GET",
url: "/api/comments"
}).success(function(users) {
var a = JSON.stringify(users);
$post.find('.commentsForTheWin span').text("Comments but not really for " + a);
})
})
});
我使用jQuery
.closest()
从.commentsInit
按钮向上遍历树,将对父级.paddingBoxForPost
的引用存储在一个变量中,并将其用于针对相关元素
$(function() {
$('.commentsInit').click(function() {
// grabs value of button which is post._id
// var uniCommID = $(this).attr("value");
var $post = $(this).closest('.paddingBoxForPost');
$post.find('.hideOnLoad').toggle("slow");
$.ajax({
type: "GET",
url: "/api/comments"
}).success(function(users) {
var a = JSON.stringify(users);
$post.find('.commentsForTheWin span').text("Comments but not really for " + a);
})
})
});
需要HTML,它不是很清楚什么和哪里需要隐藏和显示。或者清楚数据响应是什么样子。需要HTML,它不是很清楚什么和哪里需要隐藏和显示。或者清楚数据响应是什么样子。我似乎无法得到这项工作。我现在正在尝试一些不同的遍历方法,但似乎无法让它们工作either@AndrewLittle我需要更多的信息来帮助你,对不起!像蝙蝠一样瞎,看不见例子。你能设置一个JSFIDLE吗?我似乎无法完成这项工作。我现在正在尝试一些不同的遍历方法,但似乎无法让它们工作either@AndrewLittle我需要更多的信息来帮助你,对不起!像蝙蝠一样瞎,看不见例子。你能设置一个JSFIDLE吗?