单击一个按钮可取消jQuery中另一个(外部)按钮的单击事件
Clicking on One Button Cancels a Click Event of Another (Outer) Button in jQuery
我有一个允许用户发表评论的帖子,但默认情况下,评论textarea
是隐藏的,直到用户单击Comment
按钮。注释文本区域有一个 Send
和Cancel
按钮。Cancel
按钮隐藏textarea
但之后,单击Comment
按钮在刷新页面之前不会再次起作用。
HTML:
<div class='post'>
<div class='p_body'>
<div class = 'comment_body_wrapper'>
<div class = 'comment_wrapper'>
<textarea class="comment_content" rows = '2' maxlength="480" name="comment_content" placeholder="Your thoughts on this..."></textarea>
<div class = 'comment_buttons_wrapper'>
<div class='comment_buttons'>
<button class="submit_comment btn" type="submit">Send</button>
<button class="comment_cancel_button btn" type="submit">Cancel</button>
</div>
</div>
</div>
</div>
<div class = 'post_footer'>
<button class='btn post_comment' value = '1'>Comment </button>
</div>
</div>
</div>
j查询:
$('body').on('click', '.post_comment', function(){
var $this = $(this);
var $comment=$this.closest('.post').find('.comment_body_wrapper').find('.comment_wrapper');
$comment.slideToggle();
$comment.find('.comment_content').focus();
})
$('body').on('click', '.comment_cancel_button', function(){
var $this = $(this);
$this.closest('.comment_body_wrapper').hide();
})
如果我单击Comment
按钮,它肯定会切换它(显示或隐藏(,但单击Cancel
会删除此事件。The comment_cancel_button
按预期隐藏comment_wrapper
,但之后单击Comment
不会slideToggle
它。我必须刷新。我如何以正确的方式处理这个问题?
我似乎隐藏的元素与您展示的元素不同。
你藏.comment_body_wrapper
,给.comment_wrapper
您隐藏了'.comment_body_wrapper'
,但显示其子'.comment_wrapper'
:)
http://jsfiddle.net/TrueBlueAussie/mjqs6ces/1/
$(document).on('click', '.post_comment', function(){
console.log('.post_comment');
var $this = $(this);
var $comment=$this.closest('.post').find('.comment_body_wrapper .comment_wrapper');
$comment.slideToggle();
$comment.find('.comment_content').focus();
});
$(document).on('click', '.comment_cancel_button', function(){
var $this = $(this);
$this.closest('.comment_body_wrapper').find('.comment_wrapper').hide();
});
注: 您可以将两个查找缩短为一个选择器.find('.comment_body_wrapper .comment_wrapper')
"注释"按钮,您可以使用类.comment_wrapper
切换元素,但是在"取消"按钮中,您将隐藏具有不同类comment_body_wrapper
的元素。
然后你隐藏.comment_wrapper
,并尝试切换.comment_wrapper
,是一个不同的元素,所以不起作用。
在这两种情况下,您必须使用相同的类,然后它就会起作用。
$('body').on('click', '.post_comment', function(){
var $this = $(this);
var $comment=$this.closest('.post').find('.comment_body_wrapper');
$comment.slideToggle();
$comment.find('.comment_content').focus();
})
$('body').on('click', '.comment_cancel_button', function(){
var $this = $(this);
$this.closest('.comment_body_wrapper').hide();
})
- 单击按钮以等待单击按钮
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 当值更改时,在servlet中自动获取textarea值,无需单击按钮
- 单击按钮时循环浏览匹配的表
- 单击按钮后启动javascript提示
- 单击按钮时在灯箱中显示不同的内容
- 单击按钮时加载数据
- 如何在不需要单击按钮的情况下获取选项的值
- 单击按钮时显示随机字符串
- 在单击按钮前后禁用提交按钮
- 单击按钮即可在浏览器的新选项卡中打开 Sapui5 详细信息页面
- 单击按钮时Div Increment
- 若单击按钮,则更改Javascript中的变量
- 单击按钮更改加载到表中的JSON文件
- 如何在单击按钮时显示2行1列的表格
- 单击按钮前运行事件
- 如何删除父表行时;删除“;在React JS中单击按钮
- 如何创建一个表并在单击按钮时插入此标签和文本字段
- JavaScript-onClick将按钮ID或单击按钮的文本传递给函数
- 如何在单击按钮时显示一个分区,同时隐藏另一个分区