我希望只在换行符上创建一个textarea HTML元素换行符,而不会折叠前导空格.
W3C规范说CSS样式元素white-space:pre; 应该给我我想要的行为,但它不起作用 - 它不会崩溃前导空格,但它总是包装,而不仅仅是换行.
白色空间:nowrap; 除了我想要的换行符之外没有换行,但不幸的是它崩溃了领先的空白.
我需要它的唯一方法是wrap ="off" HTML属性,但W3C规范不支持.它工作得很好,似乎适用于我尝试过的所有浏览器.令我困扰的是它没有得到规范的支持.
任何人都可以告诉我为什么会出现这种情况并提供W3C支持的方法吗?也许这是我忽略的其他东西,如某些其他样式元素或某事的默认值?也许有一种使用JavaScript的方法?
W3C规范中的相关部分可在此处找到.
以下是一些显示问题的代码:
<textarea style="white-space: pre; width: 400px"> This line will wrap but the spec seems to indicate it should not. SHOULD work?</textarea><br>
<textarea style="white-space: nowrap; width: 400px"> I could do this but it collapses leading whitespace unfortunately</textarea>
<textarea wrap="off" style="width: 400px;"> This works but is not part of the standard. It should be unsupported. It is the only thing that does what I …
Run Code Online (Sandbox Code Playgroud)我认为答案是否定的,除了删除对包含对象的所有引用并允许垃圾收集最终删除包含对象的所有内容。
实时示例(使用控制台查看日志输出(在 Chrome 中按 F12 等))
代码:
(function () {
var nameValue = "uninitialized";
Object.defineProperty(this, "name", {
enumerable: true,
configurable: false,
get: function () {
return nameValue;
},
set: function () {
console.log("This is a read-only property");
}
});
console.log(nameValue);
nameValue = "George";
delete this.name;
this.name = "";
console.log(this.name);
})();
Run Code Online (Sandbox Code Playgroud)