Jquery on()切换div
我正在开发一个通过ajax请求加载动态内容的博客,我正在尝试 要使用按钮隐藏/显示文章的“发表评论”表单,请执行以下操作:Jquery on()切换div,jquery,Jquery,我正在开发一个通过ajax请求加载动态内容的博客,我正在尝试 要使用按钮隐藏/显示文章的“发表评论”表单,请执行以下操作: <div id="post"> ... <ol id="update" class="timeline"> <!-- comments here --> </ol> <button class="mybutton Bhome">Commenta</button><!-- hid
<div id="post">
...
<ol id="update" class="timeline">
<!-- comments here -->
</ol>
<button class="mybutton Bhome">Commenta</button><!-- hide/show button -->
<div class="mycomment"><!-- div to hide/show -->
<form action="#" method="post">
Nome:
<input type="text" size="12" id="name" /><br />
Email:
<input type="text" size="12" id="email" /><br />
<textarea rows="10" cols="50" class="mytext" id="commentArea"></textarea><br />
<input type="submit" class="Bhome" value=" Post " />
</form>
</div><!-- mycomment -->
</div><!-- post -->
我希望只有单击的隐藏/显示按钮对相关文章有效,但如果我的页面上有多篇文章,则带有
.mybutton
类的按钮将隐藏或显示所有文章的所有评论形式。如果您始终有一个.mybutton
按钮,然后是您指定的.mycomment
div要隐藏,只需找到下一个兄弟姐妹。mycomment
,按如下按钮:
$("div").on("click", ".mybutton", function(e){
$(this).next(".mycomment").slideToggle("slow");
e.stopPropagation();
});
注意使用
next()
方法查找下一个.mycomment
元素,该元素是单击的.mybutton
的同级元素?我会猜到的,但这不可能是对的,因为每页最多只能有一个这样的内容。我的博客是如此的实验性……)我使用mysqli获取所有文章并为html构建字符串。。。对我想我每页只放一篇文章…所以,如果你最后每页只放一篇文章,你就不需要回答这个问题了,对吗?还是我还遗漏了什么?是的。。on()函数比我在过去尝试过的live()更难使用。我想知道为什么我必须将click事件绑定到文档的所有div。。而不是特定的一个。你认为建立博客最常见的方法是每页写一篇文章吗?
$("div").on("click", ".mybutton", function(e){
$(this).next(".mycomment").slideToggle("slow");
e.stopPropagation();
});