我目前正在为我的网站创建一个简单的论坛系统,我希望用户能够互相引用。
我通过while() loop
使用php来显示每个帖子:
while ($post = $p->fetch(PDO::FETCH_ASSOC)) {
//Post Content in here.
<p class='post-text'>$text</p>
<a href='javascript:void(0)' class='quote'>Quote</a>
}
上面的代码将示例输出:
<p class='post-text'>Post 1</p>
<p class='post-text'>Post 2</p>
当用户引用其中一个帖子时,我想获取的内容.post-text
。如何做到这一点?
目前,我只有这个:
$('.quote').click(function() {
alert($('.post-text').text());
});
这将输出所有 .post-text
内容:
post1post2
现在,假设有多个帖子-.post-text
根据引用的帖子如何获取的内容?
一个简单的解决方案是将帖子ID包括在内:
while ($post = $p->fetch(PDO::FETCH_ASSOC)) {
//Post Content in here.
<p class='post-text' id='post-". $post->id ."'>$text</p>
<a href='javascript:void(0)' class='quote' data-id='post-". $post->id ."'>Quote</a>
}
然后我JS:
$('.quote').click(function() {
alert($('#post-'+ $(this).data('id')).text());
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句