HTML 选择选项中的换行符?
我可以在 html 选择选项中包含两行文本吗?如何?
Can I have a two line text in an html select option? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我可以在 html 选择选项中包含两行文本吗?如何?
Can I have a two line text in an html select option? How?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(14)
我知道这是一篇较旧的文章,但我将其留给其他将来查看的人。
您不能将换行符格式化为选项;但是,您可以使用标题属性提供鼠标悬停工具提示以提供完整信息。在选项文本中使用简短的描述符,并在标题中给出完整的内容。
I know this is an older post, but I'm leaving this for whomever else comes looking in the future.
You can't format line breaks into an option; however, you can use the title attibute to give a mouse-over tooltip to give the full info. Use a short descriptor in the option text and give the full skinny in the title.
将长文本放在正下方的另一个
中并禁用它怎么样?对于在这里发帖的人来说可能是一个解决方法。
How about putting the long text in another
<option>
right below and disabling it? Might be a workaround for someone so posting here.不,浏览器不提供此格式选项。
您可能可以使用一些带有
的复选框来伪造它,并使用 JS 将其变成飞出菜单。
No, browsers don't provide this formatting option.
You could probably fake it with some checkboxes with
<label>
s, and JS to turn it into a fly out menu.有点黑客,但这给出了多行选择的效果,为多行添加灰色背景颜色,
如果您选择任何灰色文本,它将选择第一个分组。
我想说有点聪明:)
第一个选项还展示了如何为选项添加标题标签。
A bit of a hack, but this gives the effect of a multi-line select, puts in a gray bgcolor for your multi line,
and if you select any of the gray text, it selects the first of the grouping.
Kinda clever I'd say :)
The first option also shows how you can put a title tag in for an option as well.
HTML 代码
Javascript 代码
CSS 代码
并且您必须添加一些 Jquery 库
选择框 Jquery CSS
Jquery Ui Min JS
SelectBox Js
请检查此链接
JsFiddle 链接
HTML Code
Javascript Code
CSS Code
and you have to add some Jquery Library
select Box Jquery CSS
Jquery Ui Min JS
SelectBox Js
Please check this link
JsFiddle Link
如果您使用 select2,您可以轻松构建一些适合 下拉选项 和所选选项 如下所示:
当您的文本选项由
|
分割那么您的脚本可能如下所示:
您可以在下面看到结果:
If you're using select2 you can easily build something to fit the dropdown options and selected option likw below:
When your text option is splitted by
|
Then your script could be just like this one:
The result you can see below:
只需像这样样式选择控件即可:
结果:
Just style the select control like this:
Result:
您可以使用带有列表属性的普通输入元素,然后添加带有值的选项,
该值将显示为第一行,选项文本将自动显示为第二行。我找到了这个解决方案,看看这个方法也可能有帮助
但这仅适用于 Chrome,其他浏览器不显示值文本。
you can use normal input element with list attribute, and then add options with value,
the value will be displayed to as a first line and the option text will be displayed as second line automatically. I found this solution, have a look, this way may also help
but this only works in Chrome, other browsers don't show value text.
无法在所有浏览器上完全正常工作(hr行部分),但解决方案如下:
Does not work fully (the hr line part) on all browsers, but here is the solution:
相反,也许尝试用标记替换选择,例如
Instead, maybe try replacing the select with markup, e.g.
好的,我找到了一个解决方案:
HTML:
CSS:
jQuery:
Ok i found a solution:
HTML:
CSS:
jQuery:
您可以使用名为 select2 的库
您也可以查看此 Stackoverflow 问题 &答案
在 JavaScript 中
You can use a library called select2
You also can look at this Stackoverflow Question & Answer
In javascript
一个想法可能是使用 optgroup。就我而言,发现它比禁用方法更好。我认为这比看到禁用选项更容易让用户感到困惑。
外部组件听起来很酷,比如 Vue Select,但我现在想坚持使用原生 html select。
An idea could be to use the optgroup. In my case found it better than the disabled approach. It's less confusing for the user than seeing the disabled option I think.
An external component sounds cool like Vue Select, but I wanted to stick with the native html select at the moment.
是的,通过使用 css 样式
空白:预换行;
在引导程序的 .dropdown 类中覆盖它。
早些时候它是空白:nowrap;所以它使下拉菜单包装成一行。
预包裹使其根据宽度。
yes, by using css styles
white-space: pre-wrap;
in the .dropdown class of the bootstrap by overriding it.
Earlier it is white-space: nowrap; so it makes the dropdown wrapped into one line.
pre-wrap makes it as according to the width.