也许这是一个简单的问题,也许不是。我有一个选择框,我在其中对宽度进行硬编码。说 120 像素。

<select style="width: 120px">
  <option>REALLY LONG TEXT, REALLY LONG TEXT, REALLY LONG TEXT</option>
  <option>ABC</option>
</select>

我希望能够显示第二个选项,以便用户可以看到文本的完整长度。

就像其他一切一样。这在 Firefox 中工作正常,但不适用于 Internet Explorer6。

有帮助吗?

解决方案

如果您的选项预先存在于固定版本中 <select>, ,并且您不想以编程方式更改宽度,除非您有一点创意,否则您可能会运气不好。

  • 您可以尝试设置 title 每个选项的属性。这是非标准 HTML(如果您关心这里的这个小违规行为),但 IE(以及 Firefox)将在鼠标悬停时在鼠标弹出窗口中显示整个文本。
  • 当用户选择某些内容时,您可以使用 JavaScript 在某个定位的 DIV 中显示文本。恕我直言,这是 不太好 这样做的方法,因为它需要 JavaScript 才能工作,而且它只工作 已选择某些内容 - 在值发生更改之前,选择框不会触发任何事件。
  • 您根本不使用选择框,但是 使用其他标记和 CSS 实现其功能. 。不是我最喜欢的,但我想提一下。

如果您稍后通过 JavaScript 添加长选项,请查看此处: 如何在 IE 中动态更新 HTML“选择”框

其他提示

我用以下代码解决了我的问题:

<div style="width: 180px; overflow: hidden;">
   <select style="width: auto;" name="abc" id="10">
     <option value="-1">AAAAAAAAAAA</option>
     <option value="123">123</option>
   </select>
</div>

希望它有所帮助!

干杯, Cychan

这模仿了您寻找的大部分行为:       

  <!--

     I found this works fairly well.

  -->

  <!-- On page load, be sure that something else has focus. -->
  <body onload="document.getElementById('name').focus();">
  <input id=name type=text>

  <!-- This div is for demonstration only.  The parent container may be anything -->
  <div style="height:50; width:100px; border:1px solid red;">

  <!-- Note: static width, absolute position but no top or left specified, Z-Index +1 -->
  <select
   style="width:96px; position:absolute; z-index:+1;"
   onactivate="this.style.width='auto';"
   onchange="this.blur();"
   onblur="this.style.width='96px';">
  <!-- "activate" happens before all else and "width='auto'" expands per content -->
  <!-- Both making a selection and moving to another control should return static width -->

  <option>abc</option>
  <option>abcdefghij</option>
  <option>abcdefghijklmnop</option>
  <option>abcdefghijklmnopqrstuvwxyz</option>

  </select>

  </div>

  </body>

  </html>

这将覆盖一些按键行为。

将它放在div中 并给它一个id

<div id=myForm>

然后创建一个非常简单的css来配合它。

#myForm select { 
width:200px; }

#myForm select:focus {
width:auto; }

这就是你所需要的一切。

我通过将min-width和max-width设置为select中的相同值然后将select:focus设置为auto来将其固定在我的引导页面中。

select {
  min-width: 120px;
  max-width: 120px;
}
select:focus {
  width: auto;
}
<select style="width: 120px">
  <option>REALLY LONG TEXT, REALLY LONG TEXT, REALLY LONG TEXT</option>
  <option>ABC</option>
</select>

我用于IE中现有网站的简单解决方案(使用jQuery,但如果你真的不熟悉JS,我可以用eventListener代码发回)如下:

if (jQuery.browser.msie) {
  jQuery('#mySelect').focus(function() {
    jQuery(this).width('auto');
  }).bind('blur change', function() {
    jQuery(this).width('100%');
  });
};

当然,使用变量(var cWidth = jQuery('#mySelect').width();)来存储前一个宽度,但这正是我们按照您的预期工作所需的全部。

样品

function PopulateDropdown() {
    $.ajax({
        type: "POST",
        url: "../CommonWebService.asmx/GetData",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $("select[id^='MyDropDown']").empty();
            $.each(msg.d, function () {
                $("select[id^='MyDropDownSelect']").append($("<option></option>").val(this['IdIndexDataType']).html(this['DataTypeName']));
            }); 
            $("select[id^='MyDropDown']").css("width", "auto");  
        },
        error: function (e1) {
            alert("Error - " + e1.toString());
        }
    });
}

以下代码将通过在将数据插入下拉列表后添加此代码来解决您的下拉列表宽度问题。

$("select[id^='MyDropDown']").css("width", "auto");

好的,这个选项非常hackish但应该有用。

$(document).ready( function() {
$('#select').change( function() {
    $('#hiddenDiv').html( $('#select').val() );
    $('#select').width( $('#hiddenDiv').width() );
 }
 }

哪种情况需要隐藏的div。

<div id="hiddenDiv" style="visibility:hidden"></div>
哦,你需要jQuery

我改进了cychan的解决方案,就像那样:

<html>
<head>

<style>
    .wrapper{
        display: inline;
        float: left; 
        width: 180px; 
        overflow: hidden; 
    }
    .selectArrow{
        display: inline;
        float: left;
        width: 17px;
        height: 20px;
        border:1px solid #7f9db9;
        border-left: none;
        background: url('selectArrow.png') no-repeat 1px 1px;
    }
    .selectArrow-mousedown{background: url('selectArrow-mousedown.png') no-repeat 1px 1px;}
    .selectArrow-mouseover{background: url('selectArrow-mouseover.png') no-repeat 1px 1px;}
</style>
<script language="javascript" src="jquery-1.3.2.min.js"></script>

<script language="javascript">
    $(document).ready(function(){
        $('#w1').wrap("<div class='wrapper'></div>");
        $('.wrapper').after("<div class='selectArrow'/>");
        $('.wrapper').find('select').mousedown(function(){
            $(this).parent().next().addClass('selectArrow-mousedown').removeClass('selectArrow-mouseover');
        }).
        mouseup(function(){
            $(this).parent().next().removeClass('selectArrow-mousedown').addClass('selectArrow-mouseover');
        }).
        hover(function(){
            $(this).parent().next().addClass('selectArrow-mouseover');
        }, function(){
            $(this).parent().next().removeClass('selectArrow-mouseover');
        });

        $('.selectArrow').click(function(){
            $(this).prev().find('select').focus();
        });

        $('.selectArrow').mousedown(function(){
            $(this).addClass('selectArrow-mousedown').removeClass('selectArrow-mouseover');
        }).
        mouseup(function(){
            $(this).removeClass('selectArrow-mousedown').addClass('selectArrow-mouseover');
        }).
        hover(function(){
            $(this).addClass('selectArrow-mouseover');
        }, function(){
            $(this).removeClass('selectArrow-mouseover');
        });
    });

</script>
</head>
<body>
    <select id="w1">
       <option value="0">AnyAnyAnyAnyAnyAnyAny</option>
       <option value="1">AnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAny</option>
    </select>

</body>
</html>

css课程中使用的PNG将在此处上传...

你还需要JQuery .....

你可以尝试使用css解决。通过添加类来选择

select{ width:80px;text-overflow:'...';-ms-text-overflow:ellipsis;position:absolute; z-index:+1;}
select:focus{ width:100%;}

更多参考列表框样式特定项目(选项)HTML

多选列表框

很老的问题,但这是解决方案。在这里,您有一个使用jquery的工作代码段。它使用临时辅助select复制主选择中的选定选项,以便可以评估主要<=>应该具有的真实宽度。

$('select').change(function(){
  var text = $(this).find('option:selected').text()
  var $aux = $('<select/>').append($('<option/>').text(text))
  $(this).after($aux)
  $(this).width($aux.width())
  $aux.remove()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
  <option>REALLY LONG TEXT, REALLY LONG TEXT, REALLY LONG TEXT</option>
  <option>ABC</option>
</select>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top