【问题标题】:How to increase height of jQueryUI selectmenu?如何增加 jQueryUI 选择菜单的高度?
【发布时间】:2022-01-24 00:09:50
【问题描述】:

如何增加带有 jQ​​uery 用户界面样式的选择菜单的高度?我想增加实际元素的高度,而不是单击它时出现的选项下拉菜单的高度(网站上已经有一些答案)。基本上,我希望菜单看起来是方形的,几乎就像一个按钮。

我尝试在将 UI 应用到 javascript 中的元素时设置高度。但是,我使用的任何高度值似乎都被忽略了:

$(document).ready(function() {
    
  let testSelect = document.createElement('select');
  let firstOption = document.createElement('option');
  firstOption.text = 'blablablabla...';
  firstOption.defaultSelected = true;
  testSelect.appendChild(firstOption);
  
  document.getElementById('testDiv').appendChild(testSelect);
  $(testSelect).selectmenu({height: '50%'}); // no matter what value of height I use, the menu does not change

  testButton = document.createElement('button');
  testButton.innerHTML = 'Like this example button';
  $(testButton).button();
  document.getElementById('testDiv').appendChild(testButton); // I want the select menu to look similar to a button like this
    
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.13.0/themes/redmond/jquery-ui.css"> 

<div id="testDiv" style="width:100px; height:100px;"></div>

【问题讨论】:

    标签: javascript html css jquery-ui


    【解决方案1】:

    这是我书中的一个 CSS 问题。 jQueryUI 团队为该元素使用了一个跨度,它是内联的。你需要先改变它。我们将使用display: flex,以便我们可以轻松对齐子项。然后设置高度并通过对齐项目到中心来处理文本位置。

    $(document).ready(function() {
    
      let testSelect = document.createElement('select');
      let firstOption = document.createElement('option');
      firstOption.text = 'blablablabla...';
      firstOption.defaultSelected = true;
      testSelect.appendChild(firstOption);
    
      document.getElementById('testDiv').appendChild(testSelect);
      $(testSelect).selectmenu();
    });
    .ui-selectmenu-button.ui-button {
      display: flex;
      align-items: center;
      height: 100px;
    }
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.13.0/themes/redmond/jquery-ui.css">
    
    <div id="testDiv" style="width:100px; height:100px;"></div>

    【讨论】:

      【解决方案2】:

      选择元素设置为display:none,因此操作高度和宽度不会产生任何影响。你需要修改span角色combobox的兄弟元素的高度和宽度:

      $(document).ready(function() {
      
        let testSelect = document.createElement('select');
        let firstOption = document.createElement('option');
        firstOption.text = 'blablablabla...';
        firstOption.defaultSelected = true;
        testSelect.appendChild(firstOption);
      
        document.getElementById('testDiv').appendChild(testSelect);
        $(testSelect).selectmenu({
          height: '50%'
        }); // no matter what value of height I use, the menu does not change - this is because the select is set to display:none
      
        //set the height/width of the sibling span
        $(testSelect).siblings('span').css({
          height: '150px'
        });
      });
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <script type="text/javascript" src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
      <link rel="stylesheet" href="http://code.jquery.com/ui/1.13.0/themes/redmond/jquery-ui.css">
      
      <div id="testDiv" style="width:100px; height:100px;"></div>

      【讨论】:

        【解决方案3】:

        您忘记使用.css() 函数添加高度的css。添加.css(height:100px) 然后你就会得到你的身高

        【讨论】:

        • 不。这行不通;
        • $(testSelect).css({height: '100px'});添加这一行
        • @mirzairtiza,请不要在 cmets 中转储代码。修改您的答案,并使用编辑器正确格式化您的代码。拨打tour 了解本网站的运作方式。
        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 2021-09-25
        • 2012-08-29
        • 1970-01-01
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        • 2011-05-08
        • 2014-10-18
        • 2019-05-06
        相关资源
        最近更新 更多