拆分BUtton无效

时间:2017-03-27 07:37:31

标签: javascript html css html5

我在我的应用程序中使用此示例。 https://codepen.io/kushalpandya/pen/IAhin/ 按钮完美地出现在屏幕上。我按顺序放置div部分和所有css。现在,当我点击箭头按钮时,不知道为什么选项没有显示。 我的分割按钮无效。



$(function() {
  var splitBtn = $('.x-split-button');

  $('button.x-button-drop').on('click', function() {
    if (!splitBtn.hasClass('open'))
      splitBtn.addClass('open');
  });

  $('.x-split-button').click(function(event) {
    event.stopPropagation();
  });

  $('html').on('click', function() {
    if (splitBtn.hasClass('open'))
      splitBtn.removeClass('open');
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="containerButton">
  <span class="x-split-button">
    <button class="x-button x-button-main">&#10070; Action</button>
    <button class="x-button x-button-drop">&#9660;</button>
      <ul class="x-button-drop-menu">
      <li>
        <a href="#">Item - 1</a>
      </li>
      <li>
        <a href="">Item - 2</a>
      </li>
      <li>
        <a href="">Long Item - 3</a>
      </li>
      </ul>
  </span>
</div>
&#13;
&#13;
&#13;

我是否正确放置了<Script>代码。

1 个答案:

答案 0 :(得分:2)

你也需要添加CSS!检查一下:

$(function() {
  var splitBtn = $('.x-split-button');

  $('button.x-button-drop').on('click', function() {
    if (!splitBtn.hasClass('open'))
      splitBtn.addClass('open');
  });

  $('.x-split-button').click(function(event) {
    event.stopPropagation();
  });

  $('html').on('click', function() {
    if (splitBtn.hasClass('open'))
      splitBtn.removeClass('open');
  });
});
.container {
    text-align: center;
}

.container > h2 {
    text-align: center;
}

.x-split-button {
  position: relative;
  display: inline-block;
  text-align: left;
  margin-top: 20px;
}

.x-button {
  position: relative;
  margin: 0;
  height: 30px;
  float: left;
  outline: none;
  line-height: 27px;
  background: #F2F2F2;
  border: 1px solid #E0E0E0;
  box-shadow: 1px 1px 2px #E0E0E0;
}

.x-button:hover {
  cursor: pointer;
  background: #E0E0E0;
}

.x-button:active {
  background: #D3D3D3;
}

.x-button.x-button-drop {
  border-left: 0;
  height: 30px;
}

.open > .x-button-drop-menu {
  display: block;
}

.x-button-drop-menu {
  position: absolute;
  top: 27px;
  right: 0;
  z-index: 1000;
  display: none;
  float: left;
  min-width: 160px;
  padding: 5px 0;
  margin: 2px 0 0;
  font-size: 14px;
  list-style: none;
  background-color: #F2F2F2;
  background-clip: padding-box;
  border: 1px solid #E0E0E0;
  box-shadow: 1px 1px 2px #E0E0E0;
}

.x-button-drop-menu li a {
  display: block;
  padding: 3px 20px;
  clear: both;
  font-family: arial;
  color: #444;
  text-decoration: none;
}

.x-button-drop-menu li a:hover {
  background: #D3D3D3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="containerButton">
  <span class="x-split-button">
    <button class="x-button x-button-main">&#10070; Action</button>
    <button class="x-button x-button-drop">&#9660;</button>
      <ul class="x-button-drop-menu">
      <li>
        <a href="#">Item - 1</a>
      </li>
      <li>
        <a href="">Item - 2</a>
      </li>
      <li>
        <a href="">Long Item - 3</a>
      </li>
      </ul>
  </span>
</div>