[英]how to select a class when it is used multiple times in the site
在多个地方使用特定类别时如何选择。 我想在单击“ comnt-area-view类”时显示“ disp-comment类”,comnt-area-view类和disp-comment类在文档中使用了2次
我尝试使用$(this).next,$(this).find(className)和$(clasName,this),但是它不起作用
<script>
$(document).ready(function(){
$(".disp-comment").hide();
$(".comnt-area-view").click(function(){
alert("hello");
$(this).next(".disp-comment").show()
});
});
</script>
<html>
<a class="btn-read-more comnt-area-view ">Add Comment</a><br/>
<div class="user-name disp-comment">
<input type="text" class="form-control cmnt-namefld" placeholder="Enter your name" >
<textarea class="cmnt-txtarea" rows="4" cols="40" name="comment" form="usrform"></textarea>
<input type="button" class="cmnt-btn" value="comment">
</div>
<a class="btn-read-more comnt-area-view ">Add Comment</a><br/>
<div class="user-name disp-comment">
<input type="text" class="form-control cmnt-namefld" placeholder="Enter your name" >
<textarea class="cmnt-txtarea" rows="4" cols="40" name="comment" form="usrform"></textarea>
<input type="button" class="cmnt-btn" value="comment">
</div>
</html>
问题是因为next()
元素形式.comnt-area-view
是<br />
,而不是.disp-comment
元素。
有两个解决方案。 第一种方法是删除<br />
元素并使.comnt-area-view
元素在CSS中display: block
:
.comnt-area-view {
display: block;
}
第二个是改为使用nextAll()
:
$(this).nextAll('.disp-comment').first().show();
您必须添加一个父容器,相对于它,您必须找到所需的区域,如下所示:
$(document).ready(function() { $(".disp-comment").hide(); $(".comnt-area-view").click(function() { alert("hello"); $(this).parent('.aParentDiv').find(".disp-comment").show(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="aParentDiv"> <a class="btn-read-more comnt-area-view ">Add Comment</a><br/> <div class="user-name disp-comment"> <input type="text" class="form-control cmnt-namefld" placeholder="Enter your name"> <textarea class="cmnt-txtarea" rows="4" cols="40" name="comment" form="usrform"></textarea> <input type="button" class="cmnt-btn" value="comment"> </div> </div> <div class="aParentDiv"> <a class="btn-read-more comnt-area-view ">Add Comment</a><br/> <div class="user-name disp-comment"> <input type="text" class="form-control cmnt-namefld" placeholder="Enter your name"> <textarea class="cmnt-txtarea" rows="4" cols="40" name="comment" form="usrform"></textarea> <input type="button" class="cmnt-btn" value="comment"> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.