换行符和回车符……一定是编码中最复杂的部分。 (为我)
将此代码放在页面中(来自数据库,存储为This from Ricardo\nAnd also a test\nRent 3000.00
):
<td title="This from Ricard
And also a test
Rent 3000.00" style="width:198px;text-align:left">....</td>
然后用它来获取带有 'hidden' \n's 的标题属性
var v = $('#'+i).parent().attr('title'); // i = id of <span> child of <td>
一路上,换行符丢失并在文本输入框中使用变量 v:
This from RicardAnd also a testRent 3000.00
需要什么来保存\n's
并让它看起来像下面这样?
换行符仍然存在,只是浏览器尝试渲染它们。
如果您仍然想看到“\n”,请尝试以下操作:
var v = $('td:first-of-type').attr('title').replace(/\n/g, "\\n");
否则,如果您想要的不是看到“\n”而是看到换行符,您可以这样做:
var v = $('td:first-of-type').attr('title').replace(/\n/g, "<br/>");
检查这个JSBin http://jsbin.com/awucuw/6/edit
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)