如何显示很长的选择选项的所有文本? [英] How to show all text of very long select option?

查看:73
本文介绍了如何显示很长的选择选项的所有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在<select>框中有一些很长的<option>,但是我不希望该框这么宽.问题是,当我将框的样式设置为较小时,无法阅读全文.我正在考虑将文本的副本完全悬停在鼠标指针悬停的选项上,但前提是文本太长而无法完全显示.

I have some very long <option>s in a <select> box but I don't want the box to be so wide. The problem is that the full text can't be read when I style the box smaller. I was thinking about hovering a copy of the text exactly over the option the mouse pointer is over but only if the text is too long to be fully displayed.

但是,如果我创建一个新元素,用jQuery说<p>,直到将其插入文档之前它没有宽度,所以我无法决定是否插入它.

However, if I create a new element, say a <p> with jQuery it doesn't have a width until I insert it into the document so I can't decide whether to insert it or not.

即使我成功创建了它,也不确定所有样式是否相同.

Even if I did create it successfully I'm not sure all the styles would be the same.

是否有办法使这个想法生效,或者有更好的方法来显示长<option>的完整文本?

Is there a way to get this idea working or is there a better way of displaying the complete text of the long <option>s in-place?

推荐答案

这是怎么回事?

<select id="muchtextselect">
  <option value="" title="This is some long text text This is some long text text This is some long text text ">This is some long text text This is some long text text This is some long text text </option>
  <option value="">Short Text</option>
  <option value="" title="This is some really, really long text">This is some really, really long text</option>
</select>

js

var maxLength = 15;
$('#muchtextselect > option').text(function(i, c) {
  if (c.length > maxLength) {
    return c.substr(0, maxLength) + '...';  
  }
});

工作演示

WORKING DEMO

这篇关于如何显示很长的选择选项的所有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆