我希望我的onclick函数在用户单击编辑帖子链接时作出响应。目前只有第一个编辑帖子链接正在工作。不知道有什么问题,因为jQuery应该使用post类检索所有元素,然后检索具有交互类的所有元素,最后检索其后代的第三个锚元素,并在用户单击链接时使用控制台消息进行响应。使用jQuery选择器编辑帖子
$('.post').find('.interaction').find('a').eq(2).on('click', function() {
console.log('It works!');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="row posts">
<div class="col-md-6 col-md-offset-3">
<header><h3>What other people say...</h3></header>
<article class="post">
<p>Post for edit</p>
<div class="info">
Posted by danny on 2017-01-16 17:13:17
</div>
<div class="interaction">
<a href="#">Like</a> |
<a href="#">Dislike</a> |
<a href="#">Edit</a> |
<a href="http://urlproject.dev/delete-post/8">Delete</a>
</div>
</article>
<article class="post">
<p>Another post!</p>
<div class="info">
Posted by danny on 2017-01-16 17:12:57
</div>
<div class="interaction">
<a href="#">Like</a> |
<a href="#">Dislike</a> |
<a href="#">Edit</a> |
<a href="http://urlproject.dev/delete-post/7">Delete</a>
</div>
</article>
<article class="post">
<p>I'm None</p>
<div class="info">
Posted by none on 2017-01-12 21:42:44
</div>
<div class="interaction">
<a href="#">Like</a> |
<a href="#">Dislike</a> |
</div>
</article>
</div>
</section>
止跌”如果给那些'a'元素一个体面的类名称会更好? – xzoert
是的,你是对的! –