【问题标题】:HTML Select options width is cropped in IE. Any solutions?HTML 选择选项宽度在 IE 中被裁剪。有什么解决办法吗?
【发布时间】:2010-11-04 07:04:15
【问题描述】:

在 Mozilla 和非 IE 浏览器中,如果选择列表选项的长度大于选择的宽度,它将显示出来。但在 IE 中,它会将选项裁剪为选择的宽度。

有没有办法让 IE 的选择行为与非 IE 浏览器一样?

【问题讨论】:

标签: html internet-explorer html-select


【解决方案1】:

【讨论】:

  • 欢迎来到 Stack Overflow!虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
【解决方案2】:

我放置在一个表格单元格中,只是想保持与单元格相同的宽度。

$("#id" ).width($("#id" ).parent().width());

顺便说一句,感谢所有帖子,它对我有很大帮助。

【讨论】:

    【解决方案3】:

    我在表格单元格中有选择,下面的代码对我有用:

    .myClass{
       width: 100%;//most of browsers
       _width: auto;//ie6 hack
    }
    

    干杯

    【讨论】:

      【解决方案4】:
      for (i=1;i<=5;i++){
             idname = "Usert" + i;
             document.getElementById(idname).style.width = "100%";
      
      }
      

      当宽度显示不正确时,我使用这种方式显示下拉列表。

      它适用于 IE6、Firefox 和 Chrome。

      【讨论】:

      • 这并没有改变他的帖子可能对某人有所帮助的事实。你知道,人们仍然可以用谷歌搜索这个页面——即使经过很长时间......
      【解决方案5】:

      这似乎在强制 IE 重新评估选择宽度方面效果很好。适用于 IE7 和 IE8。

      if (jQuery.browser.msie) {
          jQuery("#" + selectID).css("width", "100%").css('width', 'auto');
      }
      

      【讨论】:

      • $.browser 已被删除,我相信
      【解决方案6】:

      我喜欢 26design 的解决方案,但它有两个缺陷:

      1. 在某些情况下,可能会触发两次 mouseover 事件,从而导致 origWidth 变量设置为“auto”,从而阻止选择恢复为原始大小。

      2. 如果下拉列表中的文本值不需要额外的空间,则选择实际上会缩小,这看起来很奇怪。

      这个版本修复了这两个问题:

      if($.browser.msie) {
          /* IE obscures the option text for fixed-width selects if the text 
           * is longer than the select. To work around this we make the width 
           * auto whenever the drop down is visible.
           */
          $("select.fixed-width").livequery(function(){
              /* Use mousedown rather than focus because the focus event gets 
               * interrupted and the dropdown does not appear 
               */
              $(this)
              .mousedown(function(){
                  if($(this).css("width") != "auto") {
                      var width = $(this).width();
                      $(this).data("origWidth", $(this).css("width"))
                             .css("width", "auto");
                      /* if the width is now less than before then undo */
                      if($(this).width() < width) {
                          $(this).css("width", $(this).data("origWidth"));
                      }
                  }
              })
              /* Handle blur if the user does not change the value */
              .blur(function(){
                  $(this).css("width", $(this).data("origWidth"));
      
              })
              /* Handle change of the user does change the value */
              .change(function(){
                  $(this).css("width", $(this).data("origWidth"));
      
              });
          });
      }
      

      在 IE6-8 中测试

      【讨论】:

      • $.browser 已被删除
      • 我还没有升级到 JQuery 1.9 所以我还没有遇到这个问题。我认为我不同意删除 $.browser 的决定。特征检测是可取的,但对于一些奇怪的 IE 错误(比如这个)并不总是可行的,所以你需要求助于浏览器检测。
      【解决方案7】:

      这是另一种对我有用的方法:

      <style>
      select.limited-width {
          width: 200px;
          position: static;
      }
      
      select.expanded-width {
          width: auto;
          position: absolute;
      }
      </style>
      
      <select class="limited-width" 
              onMouseDown="if(document.all) this.className='expanded-width';"
              onBlur="if(document.all) this.className='limited-width';"
              onChange="if(document.all) this.className='limited-width';">
      
          <option>A normal option</option>
          <option>A really long option which messes everything up</option>
      </select>
      

      通过切换到 position:absolute,您可以从页面流中删除该元素,如果您在展开时发现选择框在移动,这会有所帮助。

      我选择依靠嗅探 document.all 作为 IE 检查,因为它保持紧凑并避免需要单独的函数。如果这对您不起作用,请在 IE 条件 cmets 中定义一个函数并调用它。

      一个小错误,如果您选择相同的元素,它不会再次缩小,直到您将焦点移动到另一个元素,我称之为功能;-)

      【讨论】:

        【解决方案8】:

        我有一个适合我的解决方案,所以我想我会继续发布它(底部有一些警告)。我敢肯定,这不是 jQuery 最有效的使用方式,但它正在按照我希望的方式工作。基本上,我有一个(显然)相当长的文本字符串的时区列表。

        <!--[if IE]>
        <script type="text/javascript">
            $(document).ready(function() {
                $("select.timeZone")
                .mouseover(function() {
                    $(this)
                        .data("origWidth", $(this).css("width"))
                        .css("width", "auto")
                        .focus();
                })
                .mouseout(function() {
                    $(this)
                        .blur(function() {
                            $(this).css("width", $(this).data("origWidth"));
                        })
                        .change(function() {
                            $(this).css("width", $(this).data("origWidth"));
                        })
                });
            });
        </script>
        <![endif]-->
        

        说明:

        • 仅针对 IE 浏览器有条件地设置。
        • 此解决方案是 Chris Coyier (http://css-tricks.com/select-cuts-off-options-in-ie-fix/) 提供的脚本的修改版本。
        • 此外,此解决方案假定您和我一样,不太关心 IE6。基本功能就在那里,所以 IE6 浏览器仍然可以看到大部分的选择选项、提交表单等,但我不能再浪费时间来安抚这个小小组了。

        注意事项:

        • 选择列表在获得焦点之前具有固定宽度。
        • Onmouseout,必须先模糊或更改列表,然后再将大小设置回原来的固定宽度。我确保选择列表首先模糊或更改的原因是因为以前,一旦您的鼠标离开选择框,onmouseout 就会触发(这意味着您可以尝试选择一个选项,但它们会在您单击之前消失)。
        • 相反,在鼠标悬停时设置焦点以确保正确触发模糊事件。

        【讨论】:

          【解决方案9】:

          确保选择列表的宽度始终与需要的宽度完全一样的最简单方法是允许浏览器设置它们的宽度,即不要自己设置宽度。这种“方法”(实际上是一种非方法,因为它涉及做某事)将适用于所有曾经开发过的浏览器,甚至是 IE6。

          【讨论】:

            【解决方案10】:

            好的,这是我经过一段时间后想出的解决方案。它会根据子选项的最大宽度自动增加选择框的大小。

            <script type="text/javascript">
              window.onload = function()
              {
                var elem = document.getElementById('test');
                var values = elem.length;
                var biggest_value = 0;
                var biggest_option = '';
            
                for(var i = 0; i <= values; i++)
                {
                  if (elem.options[i])
                  {
                    var len = elem.options[i].value.length;
                  }
            
                  if (biggest_value < len)
                  {
                    biggest_value = len;
                    biggest_option = elem.options[i];
                  }
                }
            
                document.getElementById('test').style.width = biggest_option.offsetWidth + 'px';
            
            };
            </script>
            

            选择框:

            <select id="test">
             <option value="Hello">Hello</option>
             <option value="Hello Again">Hello Again</option>
             <option value="Hello Yet Again">Hello Yet Again</option>
            </select>
            

            【讨论】:

            • 谢谢,我正在使用这个例子来设置我的选择框的宽度与最宽的选项... offsetWidth 很高兴知道!
            【解决方案11】:

            css:

            .set_width { width:120px; }
            

            html:

            <select class="set_width"><option>one</option><option>two</option></select>
            

            【讨论】:

            • 这将限制为 120 px ,如果我的选项比这更长怎么办?
            • 嗯,任何选择框的默认值都是增长到它需要的宽度。如果您需要支持这种情况,您可能根本不想固定它的宽度。
            猜你喜欢
            • 2014-03-14
            • 1970-01-01
            • 2016-06-01
            • 2023-03-23
            • 2013-03-25
            • 1970-01-01
            • 2010-09-17
            • 2012-07-22
            • 1970-01-01
            相关资源
            最近更新 更多