如何扩展“选择” 用户想要选择一个选项后的选项宽度?
也许这是一个简单的问题,也许不是。 我有一个
<select style="width: 120px">
<option>REALLY LONG TEXT, REALLY LONG TEXT, REALLY LONG TEXT</option>
<option>ABC</option>
</select>
我希望能够显示第二个选项,以便用户可以看到文本的完整长度。
就像其他一切一样。 这在 Firefox 中工作正常,但对我来说不适用于其他浏览器。
Maybe this is an easy question, maybe not. I have a <select>
box where I hardcode the width
to 120px
:
<select style="width: 120px">
<option>REALLY LONG TEXT, REALLY LONG TEXT, REALLY LONG TEXT</option>
<option>ABC</option>
</select>
I want to be able to show the second option so that the user can see the full length of the text.
Like everything else. This works fine in Firefox, but doesn't work with other browsers for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我用以下代码解决了我的问题:
希望能帮助到你!
I fixed my problem with the following code:
Hope it helps!
如果您的选项预先存在于固定的
title
属性。 这是非标准 HTML(如果您关心这里的这个小违规行为),但 IE(以及 Firefox)将在鼠标悬停时在鼠标弹出窗口中显示整个文本。如果您稍后通过 JavaScript 添加长选项,请查看此处:如何在 IE 中动态更新 HTML“选择”框
If you have the option pre-existing in a fixed-with
<select>
, and you don't want to change the width programmatically, you could be out of luck unless you get a little creative.title
attribute to each option. This is non-standard HTML (if you care for this minor infraction here), but IE (and Firefox as well) will display the entire text in a mouse popup on mouse hover.If you are adding a long option later through JavaScript, look here: How to update HTML “select” box dynamically in IE
很老的问题,但这是解决方案。 这里有一个使用
jquery
的工作片段。 它利用临时辅助select
,将主选择中的选定选项复制到其中,以便可以评估主select
应具有的真实宽度。Very old question but here's the solution. Here you have a working snippet using
jquery
. It makes use of a temporary auxiliaryselect
into which the selected option from the main select is copied, such that one can assess the true width which the mainselect
should have.将其放在 div 中
并给它一个 id,
然后创建一个非常非常简单的 css 来配合它。
这就是你所需要的。
Place it in a div
and give it an id
then create a really really simple css to go with it.
That's all you need.
我通过在选择中将最小宽度和最大宽度设置为相同的值,然后将选择:焦点设置为自动来修复它。
I fixed it in my bootstrap page by setting the min-width and max-width to the same value in the select and then setting the select:focus to auto.
这模仿了您寻找的大部分行为:
这将覆盖一些按键行为。
This mimics most of the behavior your looking for:
This will override some of the key-press behavior.
尽管这个问题是 2008 年提出的,但在某些情况下您仍然希望选择框匹配当前选择的宽度而不是最长的选项。 所以这是一种现代方法:
它或多或少与 这个答案 中的原理相同,只是没有 jQuery。
selectedIndex
的文本传递给该选项。position:fixed
和visibility:hidden
样式添加到新的 select 元素。 这确保了它不会影响您的布局,但仍然可以测量其边界框。getBoundingClientRect().width
获取新的所需尺寸。Even though this question is from 2008, there are still cases where you want the select box to match the width of the current selection rather than the longest option. So here's a modern approach to it:
It's more or less the same principle like in this answer just without jQuery.
selectedIndex
to the option.position: fixed
andvisibility: hidden
styles to the new select element. This ensures, that it is not affecting your layout but its bounding box can still be measured.getBoundingClientRect().width
好吧,这个选项相当黑客,但应该可行。
这当然需要一个隐藏的 div。
哦,你需要 jQuery
Okay, this option is pretty hackish but should work.
Which would offcourse require a hidden div.
ohh and you will need jQuery
我用于 IE 中现有站点的一个简单解决方案(使用 jQuery,但如果您真的不太了解 JS,我可以使用
eventListener
代码回发)如下:当然,使用变量 (
var cWidth = jQuery('#mySelect').width();
) 来存储先前的宽度,但这就是我们的宽度按您的预期工作所需的全部。A simple solution I used for an existing site in IE (using jQuery, but I can post back with
eventListener
code if you really don't know JS that well) is the following:Of course, use a variable (
var cWidth = jQuery('#mySelect').width();
) to store the previous width, but this is all that was required for ours to work as you'd expect.示例
下面的代码将通过在将数据插入下拉列表后添加此代码来解决下拉列表宽度的问题。
Sample
The below code will solve your problem of dropdownlist width by adding this code after insertion of Data to dropdownlist.
我创建了带有自动扩展功能的样式化选择元素,仅使用最少的 JS,基于数据属性和隐藏的 CSS 伪元素。
演示:
https://codepen.io/astro87/pen/dyZerdg?editors=0010
灵感来自:
https://filamentgroup.github.io/select-css/demo/
https://codepen.io/shshaw/full/bGNJJBE
I've created styled select element with auto expand, with just a minimal JS, based on data attribute and hidden CSS pseudo element.
Demo:
https://codepen.io/astro87/pen/dyZerdg?editors=0010
Inspired by:
https://filamentgroup.github.io/select-css/demo/
https://codepen.io/shshaw/full/bGNJJBE
我改进了 cychan 的解决方案,如下所示:
CSS 类中使用的 PNG 是上传到这里...
并且您仍然需要 JQuery...
I improved the cychan's solution, to be like that:
The PNGs used in css classes are uploaded here...
And you still need JQuery.....
您可以尝试仅使用 css 来解决。 通过添加类来选择
更多参考 列表框特定项目(选项)HTML 中的样式
多选列表框
you can try and solve using css only. by adding class to select
for more reference List Box Style in a particular item (option) HTML
Multi Select List Box
这是这里最好的答案。 它是纯粹的现代原生 JavaScript。
DOM 中不需要 JQuery 或永久不可见的
其他普通 JavaScript 答案在所有情况下都无法 100% 正确运行。 此方法考虑了很多因素,例如
font
、padding
、border
等。This is the best answer here. It's pure modern vanilla JavaScript.
There's no need for JQuery or a permanent invisible
<select>
within your DOM:None of the other vanilla JavaScript answers worked 100% correctly in all cases. This method takes many things into account such as
font
,padding
,border
, etc.