jQuery相当于php的自动换行? [英] jQuery equivalent of php's wordwrap?

查看:107
本文介绍了jQuery相当于php的自动换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用jQuery来完成我可以用wordwrap()在php中做的事情的方法( http://php.net/manual/en/function.wordwrap.php )

I'm looking for a way to use jQuery to do what I can do in php with wordwrap() (http://php.net/manual/en/function.wordwrap.php)

我会使用php,但我需要在dom加载后自动进行换行-

I would use php but I need the wrapping to happen dynamically after the dom is loaded--

我对jQuery不太熟悉,无法从头开始编写这种功能,并且我已经运行了一些Google和堆栈搜索,试图找到预制的插件或已经完成的插件.

I am not familiar enough with jQuery to write this sort of function from scratch and I have run a few google and stack searches trying to find a premade plugin or something that's done it already.

有什么想法吗?还是一个起点?

Any ideas? Or perhaps a starting place?

谢谢!

为了澄清起见,我需要能够在换行符处添加html(例如</p><p>)-

for clarification I need to be able to add html at the line breaks (</p><p>, for example)--

作为php:

wordwrap($text, 8, "</p><p>", true);

推荐答案

http://james.padolsey.com/javascript/wordwrap-for-javascript/

function wordwrap( str, width, brk, cut ) {
     brk = brk || '\n';
     width = width || 75;
     cut = cut || false;

     if (!str) { return str; }

     var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

     return str.match( RegExp(regex, 'g') ).join( brk );
}

这篇关于jQuery相当于php的自动换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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