自强学堂
自强学堂:学习、分享、让你更强!
JavaScript 参考手册HTMLCSSJAVASCRIPTJQUERYSQLPHPBOOTSTRAPANGULARXML
 

HTML DOM Style whiteSpace 属性

Style 对象参考手册 Style 对象

定义和用法

whiteSpace 属性设置或返回如何处理文本中的制表符、换行符和空格符。

语法

设置 whiteSpace 属性:

Object.style.whiteSpace="normal|nowrap|pre|inherit"

返回 whiteSpace 属性:

Object.style.whiteSpace

描述
normal默认。自动忽略空格符和换行符。
nowrap忽略空格符,但是不允许换行符。
pre换行符和其他空格符会被保留。
inheritwhiteSpace 属性的值从父元素继承。


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 whiteSpace 属性。

注意:IE7 及更早的版本不支持 "inherit" 值。IE8 只有规定了 !DOCTYPE 才支持 "inherit"。IE9 支持 "inherit"。


实例

实例

保留文本中的换行符和空白符:

<html>
<head>
<script>
function displayResult()
{
document.getElementById("div1").style.whiteSpace="pre";
}
</script>
</head>
<body>

<div id="div1">
This is so me text. This is some tex t. This is some
text. This is some text. This is some text. This is some text.
</div>
<br>

<button type="button" onclick="displayResult()">Preserve whitespace/line breaks</button>

</body>
</html>

尝试一下 »


Style 对象参考手册 Style 对象