javascript - Bootstrap Dropdown 禁用调整按钮大小

标签 javascript drop-down-menu twitter-bootstrap-3

所有, 我正在尝试禁用下拉按钮的大小调整并隐藏文本溢出。

我当前的应用程序有一个引导下拉列表,其中有一些相当大的选项可供选择。我愿意..

  1. 将选择按钮保持在特定宽度。
  2. 隐藏用户选择大尺寸文本选项时发生的任何溢出。

我可以设置按钮本身的宽度,但每当我选择长度较大的选项时,文本就会溢出到按钮之外。

HTML

<div class="btn-group dropdown">
      <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Select Values <span class="caret"></span>
      </button>
      <ul class="dropdown-menu">
        <li><a href="#">Some Very Large Value</a></li>
        <li><a href="#">Some Very Large Value Some Very Large Value</a></li>
        <li><a href="#">Some Very Large Value Some Very Large Value Some Very Large Value</a></li>
      </ul>
    </div>

JavaScript

$(document).ready(function () {
            //Makes the selected value show at the top of the dropdown box
            $(".dropdown-menu li a").click(function () {
                $(this).parents(".dropdown").find('.btn').html($(this).text() + ' <span class="caret"></span>');
                $(this).parents(".dropdown").find('.btn').val($(this).data('value'));
            });
        });

jsFiddle

谢谢!

最佳答案

你可以这样做: 找到你所有的li元素并检查文本的长度并调整长度

 $('.dropdown-menu').find('li').each(function(){
      console.log( $(this).text().length );
      if($(this).text().length >= 10){
           $(this).text($(this).text().substring(0, 11) + '...')
      }
  });

jsfiddle example

关于javascript - Bootstrap Dropdown 禁用调整按钮大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40136816/

相关文章:

javascript - 如何将此数组转换为 JavaScript 对象?

javascript - 使用 javascript 自动加载 href ="#"

javascript - 复选框仅适用于第一页 - 数据表、 rails

jquery - 在 jQuery 中使用 2 个下拉菜单项

html - Bootstrap 下拉菜单在移动设备上消失

当鼠标悬停在两个重叠的 Canvas 上时,CSS 下拉菜单不起作用

css - Typo3 Neos 响应式 CSS 特性

html - 在 bootstrap 3 中创建导航栏时,我似乎无法让按钮居中

javascript - 修复多选复选框中应用按钮的位置

css - Twitter Bootstraps Yamm3 下拉菜单向右浮动...奇怪吗?