onclick删除选中并显示/隐藏删除按钮和下拉标题

时间:2016-06-27 14:18:34

标签: javascript jquery html css twitter-bootstrap

Use this jsFiddle instead of Stack Snippet

$(".dropdown li a").dblclick(function(){
  //alert('Long pressed');
  $(this).parent().toggleClass('selected');
  if ($('.dropdown li').hasClass('selected')) {
    // alert('1 Item selected');
    $(this).parent().parent().parent().parent().parent().parent().find('.header').find('.dropdown-title').addClass('hide');
    $(this).parent().parent().parent().parent().parent().parent().find('.deleteli').removeClass('hide');
    $(this).parent().parent().parent().parent().parent().parent().find('.deleteli').addClass('show');
  }
  else {
    $(this).parent().parent().parent().parent().parent().parent().find('.header').find('span').removeClass('hide');
    $('.delete').removeClass('show');
    $('.delete').addClass('hide');
    // alert('1 Item dis selected');
  }
});
$('a#deletelibtn').click(function () {
  // body...
  // alert('deletelibtn clicked')
  $(this).parent().parent().find('.selected').remove();
  $(this).parent().find('.dropdown-title').removeClass('hide');
  $(this).parent().find('.dropdown-title').addClass('show');
})
/*Dropdown Messages*/
.messages-dropdown{
    width: 300px;
}
.messages-dropdown .header{
    padding: 15px;
    border-bottom: 1px solid #ccc;
}
.messages-dropdown .header small{
    float: right;
}
.messages-dropdown .header small a{
    font-style: 9px;
    margin-left: 5px;
}
.messages-dropdown .footer{
    text-align: center;
    border-top: 1px solid #ccc;
}
.messages-dropdown .footer a{
    padding: 15px;
}
.messages-items{
    list-style: none;
    padding: 0;
    padding-left: 0px;
    margin: 0;
}
.messages-items .Mitem {
    padding: 10px;
    padding-left: 10px;
    border-left: 3px solid transparent;
}
.messages-items .Mitem:hover {
    background: #f0f0f0;
    border-color: #3a3f51;
}
.messages-items .Mitem a {
    display: inline-block;
    width: 100%;
}
.messages-items .Mitem a:hover {
    text-decoration: none;
    background: #f0f0f0;
}
.messages-items .Mitem a b{
    color: #444;
    letter-spacing: 1px;
}
.messages-items .Mitem a small{
    color: #999;
    letter-spacing: 1px;
}
.messages-items .Mitem img{
    float: left;
    margin-right: 10px;
    border-radius: 100% !important;
    padding: 5px;
    border: 1px solid #ccc;
    width: 40px;
    height: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>


<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div>
      <ul class="nav navbar-nav navbar-right">
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
            
            <i class="icon icon-li-envelope message-bell"></i>
            <span class="badge message-badge">4</span>
          </a>
          <ul class="dropdown-menu messages-dropdown">
            <li class="header">
              <span class="dropdown-title">
                heading
              </span>
              <a class="#delete" id="deletelibtn">
                delete
              </a>
            </li>
            <div id="message">
              <li>
                <ul class="messages-items">
                  <li class="Mitem">
                    <a href="#MessageItem">
                      content
                    </a>
                  </li>
                </ul>
              </li>
              <li>
                <ul class="messages-items">
                  <li class="Mitem">
                    <a href="#MessageItem">
                      content
                    </a>
                  </li>
                </ul>
              </li>
              <li>
                <ul class="messages-items">
                  <li class="Mitem">
                    <a href="#MessageItem">
                      content
                    </a>
                  </li>
                </ul>
              </li>
              <li>
                <ul class="messages-items">
                  <li class="Mitem">
                    <a href="#MessageItem">
                      content
                    </a>
                  </li>
                </ul>
              </li>
            </div>
          </ul>
        </li>
      </ul>
    </div>
    <!--/.nav-collapse -->
  </div>
</nav>
当我选择消息时,deletelibtn选项已启用,我可以删除消息,但在删除消息后,deletelibtn按钮未隐藏,如果再次选择消息则删除选项未显示。

当显示deletelibtn选项时,应隐藏dropdown-title,同样在显示dropdown-title时,应隐藏deletelibtn按钮。

我希望我能完全解释我的需要。

1 个答案:

答案 0 :(得分:0)

我改变了一些不会影响功能的小东西,但却是最佳实践&#39;。还有一些小的目标问题也是导致功能问题的原因。调试如此困难的原因是因为你的html是混乱的,你重复使用相同的单词使得很难遵循逻辑。这是纠正过的js。

$('.dropdown-menu li').on('click', function(event){
//The event won't be propagated to the document NODE and 
// therefore events delegated to document won't be fired
event.stopPropagation();
});
$("ul.messages-items a").dblclick(function(){
$(this).closest('ul.messages-items').parent().addClass('selected');
  var $container = $(this).closest('div.container-fluid')
  if ($('li.dropdown').hasClass('open')) {
    $container.find('.header').find('.dropdown-title').removeClass('show').addClass('hide');
    $container.find('.deleteli').removeClass('hide').addClass('show');
  }
  else {
      $container.find('.header').find('span').removeClass('hide').addClass('show');
      $('.delete').removeClass('show').addClass('hide');
  }
});
$('a#deletelibtn').click(function () {
    var $this = $(this);
    $('#message').find('li.selected').remove();
    $this.parent().find('.dropdown-title').removeClass('hide').addClass('show');
    $this.find('i.deleteli').removeClass('show').addClass('hide');
})

https://jsfiddle.net/xctgo71x/23/

您可以添加此关闭点击事件以重置下拉列表。

$(document).click(function(e){
  //console.log(e);
  if(true)
    ResetDropdown();
});
function ResetDropdown(){
    $('span.dropdown-title').removeClass('hide').addClass('show');
    $('i.deleteli').removeClass('show').addClass('hide');
    $('#message').find('li.selected').removeClass('selected');
}

它在jsfiddle上运行正常,但是根据你的实际代码,你可能需要调整一下。

https://jsfiddle.net/xctgo71x/29/