简体   繁体   English

jQuery-隐藏显示无法在Chrome中正常运行(只能运行一次)

[英]jQuery - Hide show not working properly in Chrome (Works only once)

Just wrote a jquery to show a comment button, when click on the textarea. 只需编写一个jQuery,以在单击文本区域时显示评论按钮。 Hide comment button clicking on somewhere else in the screen. 隐藏评论按钮,单击屏幕上的其他位置。 Its working fine in Firefox. 在Firefox中工作正常。 But in Chrome it working only once. 但是在Chrome中,它只能运行一次。 When I click on textarea again the submit button is not showing, its still hidden. 当我再次单击textarea时,提交按钮未显示,但仍处于隐藏状态。

$(document).on('click', ".comment_txt, .comment_btn", function() {
  var post_id = $(this).attr("post-id");      
  $("#comment_btn_div_"+post_id).show();
});
$('body').click(function() {
  $(".comment_btn").hide()
});


<form class="comment_submit" action="http://localhost:3000/api/v2/posts/48774/comment" data-post-id="48774" id="comment_form_48774">
    <textarea post-id="48774" id="comment_txt_48774" placeholder="Comment" cols="40" rows="1" class="width100 comment_txt"></textarea>
    <div id="comment_btn_div_48774" class="right comment_btn" post-id="48774" style="display:none">
      <button onclick="$(this).text('commenting...')" class="btn btn-small btn-info right" id="comment_btn_48774" type="submit">Comment</button>
    </div>
</form>

Not sure why this is not working in Chrome. 不知道为什么这在Chrome中不起作用。 There are many form in my page. 我的页面上有很多表格。 So I have did $(".comment_btn").hide() on body click. 所以我在单击主体时做了$(".comment_btn").hide() In order to show a particular comment button i'm using this code $("#comment_btn_div_"+post_id).show(); 为了显示特定的评论按钮,我使用此代码$("#comment_btn_div_"+post_id).show();

Update: 更新:

After the comment button is hidden, Even when i do a $("#comment_btn_div_23232").show() from firebug console. 隐藏评论按钮后,即使我从Firebug控制台执行$(“#comment_btn_div_23232”)。show()。 Its not showing the div. 它没有显示div。

Update 2 (testing with alert): 更新2(测试警报):

$(document).on('click', ".comment_txt, .comment_btn", function() {
  alert("commenttext area clicked");
  $(".comment_btn").show()
});
$('body').click(function() {
  alert("body clicked");
  $(".comment_btn").hide()
});
  1. Clicked textarea, got alert a. 单击文本区域,得到警报a。 body clicked b. 身体单击b。 commenttext area clicked. 点击了注释文本区域。 Now comment button is shown 现在显示评论按钮
  2. Clicked body got alert a. 单击的身体警报a。 body clicked. 身体点击了。 Now Comment button is hidden 现在“评论”按钮已隐藏
  3. Clicked textarea, got alert a. 单击文本区域,得到警报a。 body clicked b. 身体单击b。 commenttext area clicked. 点击了注释文本区域。 Now comment button is not shown . 现在不显示评论按钮。

Thanks! 谢谢!

Try it with blur 尝试模糊

$(document).on('click', ".comment_txt, .comment_btn", function () {
    var post_id = $(this).attr("post-id");
    $("#comment_btn_div_" + post_id).show();
});

$('.comment_txt').blur(function () {
    $(".comment_btn").hide()
});

FIDDLE 小提琴

I run into this every once in a while. 我碰到这样每过一段时间。 Have yet to figure out why it happens; 还没有弄清楚为什么会这样。 it befuddles me. 它使我迷惑。 I solve it by using one of the other ways of "showing" divs. 我通过使用“显示” div的其他方法之一来解决它。 Most recently, I changed the css display property: 最近,我更改了css display属性:

$("#comment_btn_div_"+post_id).css('display','block'); $(“#comment_btn_div _” + post_id).css('display','block');

var post_id=null;
$(document).on('click', ".comment_txt, .comment_btn", function(event) {
  post_id = $(this).attr("post-id");      
   $("#comment_btn_div_"+post_id).show();
   event.stopPropagation();

   $('body').bind("click",function() {
        $("#comment_btn_div_"+post_id).hide()
        $(this).unbind();
   });
});

I hope it will work for you 我希望它对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM