jQuery文本()和换行符 [英] jQuery text() and newlines

查看:114
本文介绍了jQuery文本()和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够说

  $(someElem).text('this \\\
has \\\
新行);

并在浏览器中使用换行符进行渲染。我发现的唯一解决方法是将css属性'white-space'设置为someElem上的'pre'。这几乎可以工作,但是在文本和someElem的顶部之间有一个令人讨厌的大填充,即使当我将填充设置为0时。是否有办法摆脱这种情况?

解决方案

现在是2015年。此问题的正确答案是使用CSS white-space:pre-line 空格:预先包装。清洁和优雅。支持这对的IE的最低版本是8。

https://css-tricks.com/almanac/properties/w/whitespace/



PS在CSS3变得常见之前,您可能需要手动修剪初始和/或尾随的空格。


I want to be able to say

$(someElem).text('this\n has\n newlines);

and it renders with newlines in the browser. The only workaround I have found is to set the css property 'white-space' to 'pre' on someElem. This almost works, but then I have an annoyingly large padding between the text and the top of someElem, even when I set padding to 0. Is there a way to get rid of this?

解决方案

It's the year 2015. The correct answer to this question at this point is to use CSS white-space: pre-line or white-space: pre-wrap. Clean and elegant. The lowest version of IE that supports the pair is 8.

https://css-tricks.com/almanac/properties/w/whitespace/

P.S. Until CSS3 become common you'd probably need to manually trim off initial and/or trailing white-spaces.

这篇关于jQuery文本()和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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