使用jQuery将长字符串拆分为文本块(Split long string into text chunks with jQuery)
我有一个长字符串需要切成数组内的单独块,预定义的长度限制块。 一些规则适用:
- 如果限制削减了一个单词,则该单词将被分隔以用于下一个块。
- 必须修剪切片(数组项的开头或结尾没有空格)。
- 特殊标点符号,!? 应该留在这个词,而不是被发送到下一个块。
原文 :我的时间完全没有受到重视。 您可以从这间客房经营整个公园,最少有3天的工作人员。 您认为那种自动化很容易吗? 还是便宜? 你知道有谁可以联网8台连接机器并调试200万行代码,以便我为这份工作买单吗? 因为如果他能让我看到他试试。
结果与当前代码 [“我完全”,“在我的时间中不被赏识”,“。你可以运行这整个”,“从这个房间停放”,“最小的工作人员,最多”,“3天。你认为“,”有点自动化是“,”sy?还是便宜?你知道“,”任何可以联网的人,“8个连接机器”,“调试200万行代码”,我的出价代码“ ,“为了这份工作?因为如果”,“我可以想看到h”,“我试试。”]
......它应该是:
[“我完全”,“在我的时间里没有得到赏识。”,“你可以运行这一整体”,“从这个房间停放”,“最少3”的工作人员,“天。你认为那种”,“自动化很容易?“,”或便宜?你知道任何人“,”谁可以网络8“,”连接机器“,”调试200万行“,”代码为我竞标“,这个工作?因为如果他“,”我想看到他“,”试试。“]
如你所见,我仍然遇到第2和第3条规则的问题。
这是我目前的代码(您可以在jsfiddle中查看工作演示 ):
function text_split(string, limit, pos, lines) { //variables if(!pos) pos = 0; if(!lines) lines = []; var length = string.val().length; var length_current; //cut string var split = string.val().substr(pos, limit); if(/^\S/.test(string.val().substr(pos, limit))) { //check if it is cutting a word split = split.replace(/\s+\S*$/, ""); } //current string length length_current = split.length; //current position pos_current = length_current + pos; //what to do if(pos_current < length) { lines.push(split); return text_split(string, limit, pos_current, lines); } else { console.log(lines); return lines; } } $(function(){ $('#button').click(function(){ text_split($('#textarea'), 25); }); });
演示的html表单:
<textarea id="textarea" rows="10" cols="80">I am totally unappreciated in my time. You can run this whole park from this room with minimal staff for up to 3 days. You think that kind of automation is easy? Or cheap? You know anybody who can network 8 connection machines and debug 2 million lines of code for what I bid for this job? Because if he can I'd like to see him try.</textarea> <button id="button">demo</button>
I have a long string that needs to be sliced into separated chunks inside an array, with a predefined length limit the chunks. Some rules apply:
- If the limit cuts a word, then the word is separated for the next chunk.
- Slices must be trimmed (no spaces at the beginning or end of the array item).
- Special punctuation .,!? should stay with the word, and not be sent to the next chunk.
Original text: I am totally unappreciated in my time. You can run this whole park from this room with minimal staff for up to 3 days. You think that kind of automation is easy? Or cheap? You know anybody who can network 8 connection machines and debug 2 million lines of code for what I bid for this job? Because if he can I'd like to see him try.
Result with current code ["I am totally", " unappreciated in my time", ". You can run this whole", " park from this room with", " minimal staff for up to ", "3 days. You think that", " kind of automation is ea", "sy? Or cheap? You know", " anybody who can network ", "8 connection machines", " and debug 2 million line", "s of code for what I bid", " for this job? Because if", " he can I'd like to see h", "im try."]
...it should actually be:
["I am totally", "unappreciated in my time.", "You can run this whole", "park from this room with", "minimal staff for up to 3", "days. You think that kind", "of automation is easy?", "Or cheap? You know anybody", "who can network 8", "connection machines and", "debug 2 million lines of", "code for what I bid for", "this job? Because if he", "can I'd like to see him", "try."]
As you can see, I'm still having trouble with rules 2 and 3.
This is my current code (you can check the working demo in jsfiddle):
function text_split(string, limit, pos, lines) { //variables if(!pos) pos = 0; if(!lines) lines = []; var length = string.val().length; var length_current; //cut string var split = string.val().substr(pos, limit); if(/^\S/.test(string.val().substr(pos, limit))) { //check if it is cutting a word split = split.replace(/\s+\S*$/, ""); } //current string length length_current = split.length; //current position pos_current = length_current + pos; //what to do if(pos_current < length) { lines.push(split); return text_split(string, limit, pos_current, lines); } else { console.log(lines); return lines; } } $(function(){ $('#button').click(function(){ text_split($('#textarea'), 25); }); });
The html form for the demo:
<textarea id="textarea" rows="10" cols="80">I am totally unappreciated in my time. You can run this whole park from this room with minimal staff for up to 3 days. You think that kind of automation is easy? Or cheap? You know anybody who can network 8 connection machines and debug 2 million lines of code for what I bid for this job? Because if he can I'd like to see him try.</textarea> <button id="button">demo</button>
原文:https://stackoverflow.com/questions/28225522
最满意答案
只需使用
vclick
而不是tap
或click
。 这是一个jQuery Mobile事件,可以解决移动和桌面问题,点击/点击不能在两个平台上运行。工作示例: http : //jsfiddle.net/2ckHr/9/
$(document).delegate("#submit", "vclick", function() { alert($("#flip-1").val()); });
Just use
vclick
instead oftap
orclick
. It is a jQuery Mobile event that bridges a mobile and desktop problems with tap/click not working on both platforms.Working example: http://jsfiddle.net/2ckHr/9/
$(document).delegate("#submit", "vclick", function() { alert($("#flip-1").val()); });
相关问答
更多-
我认为你在插件执行它之前运行你的代码(也准备就绪),首先检查.ui-state-active esxist,如果没有运行超时,再试一次 function checkState() { if ($('.ui-state-active').lenght) { alert($('.ui-state-active').html()); } else { setTimeout(function() { checkState() }, 500); } ...
-
将knockout.js绑定到布尔JQuery Mobile翻转开关切换(Bind knockout.js to a boolean JQuery Mobile flip switch toggle)[2022-12-25]
我知道了, 感谢kadumel指出我的第一段代码非常糟糕。 然后,我从计算的observable切换到自定义绑定,这在我看来是一个很好的解决方案: ko.bindingHandlers.jqmFlip = { init: function (element, valueAccessor) { var result = ko.bindingHandlers.value.init.apply(this, arguments); try { $(el ... -
您需要使用event delegation方法,因为它们是动态创建的 $('#item').on("slidestart","#flip1", function(event, ui) { alert('test'); }); You need to use event delegation method, since they are created dynamically $('#item').on("slidestart","#flip1", function(event, ui) { ...
-
只需使用vclick而不是tap或click 。 这是一个jQuery Mobile事件,可以解决移动和桌面问题,点击/点击不能在两个平台上运行。 工作示例: http : //jsfiddle.net/2ckHr/9/ $(document).delegate("#submit", "vclick", function() { alert($("#flip-1").val()); }); Just use vclick instead of tap or click. It is a jQuer ...
-
Jquery mobile:禁用“点击切换”固定页眉和页脚(Jquery mobile: Disable “tap to toggle” fixed header and footer)[2022-02-22]
默认情况下,jquery mobile自动统计页面填充。 所以它看起来对你的页面不正确,这就是为什么内容在标题下面并且看起来被剪切掉了。 您可以通过JQM禁用页面填充的自动更新,并由您自己设置。 看看这里的updatePagePadding属性: http : //jquerymobile.com/test/docs/toolbars/bars-fixed-options.html 此外,它需要做一个修复,这里描述: https : //github.com/jquery/jquery-mobile/iss ... -
在检查是否有孩子时,您需要检查长度属性。 否则,您正在检查的是结果是否为非null ...并且即使选择器不匹配也将始终为非null,因为它将返回jQuery。 它将是空的任何元素,但仍然是一个对象,因此非空。 if (element.has('ul').children('a').length > 0) { element.prepend("+ "); You need to check the length property when checking if it ha ...
-
将事件处理程序锚定到包装的某个元素。 请注意,您删除了它锚定的类,一旦发生这种情况,就会删除click事件处理程序的锚点。 示例:使用锚定到pull-right类的单个处理程序 $(".pull-right").on("click", function() { var me = $(this); if (me.hasClass('hamburger-container')) { $("#bar1").attr('class', 'bar1'); me.addClass('toggle ...
-
如何在jQuery Mobile中以编程方式切换导航栏选项卡?(How do I programmatic toggle navbar tabs in jQuery Mobile?)[2024-05-07]
jQuery Mobile NAV Toggle的jsFiddle 演示 我已经通过上面的链接创建了一个jsFiddle Demo ,它可以解决您的问题。 特别是,您可以轻松设置NAV Bar ,并将其切换到不同的酒吧。 我说不同的酒吧,因为小提琴允许你需要更多的NAV栏 ,并且很容易适应。 可以肯定的是,任何NAV条最多可以在5个按钮上使用 。 编辑:根据您的留言评论,一次创建超过1页的页面之间的导航很容易。 这个不同的jsFiddle演示了如何标签锚标签以实时切换页面。 这可以与原始的jsFiddle ... -
如何通过单击按钮切换jQuery Mobile Accordion?(How do I toggle the jQuery Mobile Accordion with a button click?)[2023-01-27]
实例: http://jsfiddle.net/h2pPz/19/ HTML:...Jquery关闭(Jquery toggle on off)[2020-05-06]
$('#check').click(function(){ if($(this).prop("checked") == true){ alert("checked."); } else if($(this).prop("checked") == false){ alert("not checked."); } }); $('# ...相关文章
更多- jquery 怎么得到link中text的属性
- jquery 问题
- jQuery表格插件jqGrid(jquery.jqGrid.js)
- Guava Longs类-long的实用工具类
- jquery与servlet交互的json问题
- 怎么得到String数组?
- jquery validate注册校验实例
- 简单的动态树Jquery Ztree实现
- Guava LongMath类-long基础类型的Math实用方法
- java Long型的比较
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)