简体   繁体   English

如何在 html 输入字段中使用文本溢出省略号?

[英]How to use text-overflow ellipsis in an html input field?

My web page has input fields to which I have applied the following css :我的网页具有应用了以下 css 的输入字段:

.ellip {
    white-space: nowrap;
    width: 200px;
    overflow: hidden;
    -o-text-overflow: ellipsis;
    -ms-text-overflow: ellipsis;
    text-overflow: ellipsis;
}

But this doesn't seem to have any effect.但这似乎没有任何影响。 Am I missing something obvious here or is it not possible to have an ellipsis in an input field using CSS only?我是否在这里遗漏了一些明显的东西,或者仅使用 CSS 在输入字段中不可能有省略号?

I know this is an old question, but I was having the same problem and came across this blog post from Front End Tricks And Magic that worked for me, so I figured I'd share in case people are still curious.我知道这是一个老问题,但我遇到了同样的问题,并从前端技巧和魔法中看到了这篇对我有用的博客文章,所以我想我会分享以防人们仍然好奇。 The gist of the blog is that you CAN do an ellipsis in an input in IE as well, but only if the input has a readonly attribute.该博客的要点是,您也可以在 IE 中的输入中使用省略号,但readonly是输入具有readonly属性。

Obviously in many scenarios we don't want our input to have a readonly attribute.显然,在许多情况下,我们不希望我们的输入具有只读属性。 In which case you can use JavaScript to toggle it.在这种情况下,您可以使用 JavaScript 来切换它。 This code is take directly from the blog, so if you find this answer helpful you might consider checking out the blog and leaving a comment of appreciation to the author.此代码直接取自博客,因此如果您发现此答案有帮助,您可以考虑查看博客并向作者发表评论。

 // making the input editable $('.long-value-input').on('click', function() { $(this).prop('readonly', ''); $(this).focus(); }) // making the input readonly $('.long-value-input').on('blur', function() { $(this).prop('readonly', 'readonly'); });
 .long-value-input { width: 200px; height: 30px; padding: 0 10px; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="long-value-input-container"> <input type="text" class="long-value-input" value="sdghkjsdghhjdfgjhdjghjdfhghjhgkdfgjnfkdgnjkndfgjknk" readonly /> </div>

Setting text-overflow:ellipsis on the input itself did the trick for me.在输入本身上设置text-overflow:ellipsis对我有用。 It truncates and places the ellipsis when the input is out of focus.当输入失焦时,它会截断并放置省略号。

From my testing Chrome, Safari and Firefox now support it.从我的测试来看,Chrome、Safari 和 Firefox 现在都支持它。

However they do have slightly different effects.但是,它们的效果确实略有不同。

On blur Firefox appends ... to the right of the text but only if there is any text hidden by the right hand side of the text box.在模糊时,Firefox 将 ... 添加到文本右侧,但前提是文本框右侧隐藏了任何文本。

Whereas on blur Chrome seems to jump to the beginning of the text and appends the ... at the end, regardless of where you left the scroll position of the text.而在 blur 上,Chrome 似乎会跳到文本的开头并在末尾附加 ...,无论您将文本的滚动位置留在何处。

A field is an element with its own rules.字段是具有自己规则的元素。 The input field is implemented in the way that if the text gets longer than the field, the text gets just unseen.输入字段的实现方式是,如果文本比字段长,则文本将不可见。

The reason for this is, if you use the field in a form and it would be possible to use text-overflow ellipsis, then it would only transmit the truncated content, what is not the wanted effect.这样做的原因是,如果您在表单中使用该字段并且可以使用文本溢出省略号,那么它只会传输截断的内容,这不是想要的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM