简体   繁体   English

用jQuery压缩文本框中的字符

[英]squeeze characters in a textbox with jquery

I have strings, eg 我有琴弦,例如

Computer > Software > Programming Language > Python
Computer > Software > Game > BT4

I want to squeeze and display like the following, if the string does not fit in the width of textbox: 如果字符串不适合文本框的宽度,我想挤压并显示如下:

Computer > Software > ...> Python
Computer > Software > Game > BT4

The strings are obtained from an AJAX response. 字符串是从AJAX响应获得的。 How can I achieve this with jQuery? 如何使用jQuery实现此目标?

This might help you solve your problem: 这可以帮助您解决问题:

https://github.com/STAR-ZERO/jquery-ellipsis https://github.com/STAR-ZERO/jquery-ellipsis

or even 甚至

http://dotdotdot.frebsite.nl/ http://dotdotdot.frebsite.nl/

Update 更新资料

Here is what I came up with: Jsfiddle 这是我想出的: Jsfiddle

 appendList = ['Computer', 'Software', 'Programming Language', 'Python']; idx = -1; $('#appendButton').on('click', function() { if (idx++ >= appendList.length-1) return; var inputField = $('#inputField'), value = inputField.val(); inputField.val(value + ((idx == 0) ? appendList[idx]:' > ' + appendList[idx])); value = inputField.val(); // Check the length based on the "size" of the input. if (value.length >= 30) { // Get the last "section" var startPos = (value.lastIndexOf('>')-1), begStr = value.slice(0, startPos - 1), saveStr = value.slice(startPos); // Now we need the last occurrence of '>', // This will give you multiple occurrences of '...' var secStartPos = begStr.lastIndexOf('>')+2; // Or you can use these two lines, instead // of the ones below if you only ever want // one '...', but to always show the last "section" // // var secStartPos = begStr.indexOf('>')+2; // while ((secStartPos = begStr.lastIndexOf('>', secStartPos)+2) >= 30) { }; // var secSaveStr = begStr.slice(0, secStartPos); // Now append '...' secSaveStr += '...'; // Rebuild the string. var finalStr = secSaveStr + saveStr; inputField.val(finalStr); } }); 

Oh yeah BTW, no clue what this does when the text box fills up with '> ... >' :) GL 哦是的,顺便说一句,当文本框用'> ...>':) GL填充时,这不知道怎么办

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

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