- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在法语版式约定中,很少有标点符号像 ;
、:
或 ?
前面必须有一个空格。当标点位于其 HTML 容器的边界时,这会导致不需要的换行。
Voulez-vous coucher avec moi ce soir ? Non, merci.
成为
Voulez-vous coucher avec moi ce soir
? Non, merci.
目标是这样得到的:
Voulez-vous coucher avec moi ce
soir ? Oui, bien sûr.
我没有找到任何方法来避免使用 CSS 在这些字符之前换行。此外:
不是一种选择。whitespace: nowrap;
不是一种选择,因为它必须在所有其他情况下发生。我正在考虑使用运行 str.replace("?", " ?");
的全局 javascript 函数,但我无法弄清楚一些事情:
有什么建议吗?
注意: Whitespace before some punctuation characters in French: is there a CSS way to avoid lines breaking?共享相同的目标,但具有我这里没有的纯 CSS 约束。
最佳答案
理想情况下,您应该用硬空间替换服务器端,而不是客户端,因为如果您在客户端执行此操作,您将遇到以下问题当您更新内容时,内容会回流,这会导致可察觉的闪烁/移动。
大多数情况下,您会在文本节点中遇到此问题,例如:
<p>Voulez-vous coucher avec moi ce soir ? Non, merci.</p>
这是一个元素(p
元素),里面有一个文本节点。
可能将它放在一个文本节点中的空格和另一个文本节点中的标点符号的位置:
<p><span>Voulez-vous coucher avec moi ce soir </span>? Non, merci.</p>
“Voulez-vous coucher avec moi ce soir”和空格在一个文本节点中(在 span
内),但标点符号在另一个文本节点中(在 p
中)。我假设这种情况非常罕见,我们无需担心。
在文本节点中处理它相对容易:
fixSpaces(document.body);
function fixSpaces(node) {
switch (node.nodeType) {
case 1: // Element
for (let child = node.firstChild;
child;
child = child.nextSibling) {
fixSpaces(child);
}
break;
case 3: // Text node
node.nodeValue = node.nodeValue.replace(/ ([;?!])/g, "\u00a0$1");
break;
}
}
实例:
// Obviously, you wouldn't have this in a `setTimeout` in your
// real code, you'd call it directly right away
// as shown in the answer
console.log("Before...");
setTimeout(() => {
fixSpaces(document.body);
console.log("After");
}, 1000);
function fixSpaces(node) {
switch (node.nodeType) {
case 1: // Element
for (let child = node.firstChild;
child;
child = child.nextSibling) {
fixSpaces(child);
}
break;
case 3: // Text node
node.nodeValue = node.nodeValue.replace(/ ([;?!])/g, "\u00a0$1");
break;
}
}
p {
font-size: 14px;
display: inline-block;
width: 220px;
border: 1px solid #eee;
}
<p>Voulez-vous coucher avec moi ce soir ? Non, merci.</p>
你会在 script
中找到它body
末尾的标签, 就在收盘前 </body>
元素。
但再次强调:如果您可以在服务器端执行此操作,那会更好,以避免回流。
如果我们想尝试处理一个 Text 节点以空格结尾而下一个以标点符号开头的情况,如我上面所示:
<p><span>Voulez-vous coucher avec moi ce soir </span>? Non, merci.</p>
...一种方法是获取所有文本节点的数组,然后查看是否有一个以空格结尾,下一个以标点符号开头。这是不完美的,因为它不会尝试处理以空格结尾的文本节点位于 block 元素末尾的情况(例如,标点符号在视觉上总是位于不同的行上),但它可能聊胜于无。唯一的缺点是你最终可能会在 block 元素的末尾有一个额外的硬空间。
fixSpaces(document.body);
function gatherText(node, array = []) {
switch (node.nodeType) {
case 1: // Element
for (let child = node.firstChild;
child;
child = child.nextSibling) {
array = gatherText(child, array);
}
break;
case 3: // Text node
array.push(node);
break;
}
return array;
}
function fixSpaces(node) {
const texts = gatherText(node);
for (let i = 0, len = texts.length; i < len; ++i) {
const text = texts[i];
const str = text.nodeValue = text.nodeValue.replace(/ ([;?|])/g, "\u00A0$1");
if (i < len - 1 && str[str.length - 1] === " " && /^[;?!]/.test(texts[i + 1].nodeValue)) {
// This node ends with a space and the next starts with punctuation,
// replace the space with a hard space
text.nodeValue = str.substring(0, str.length - 1) + "\u00A0";
}
}
}
console.log("Before...");
setTimeout(() => {
fixSpaces(document.body);
console.log("After");
}, 1000);
function gatherText(node, array = []) {
switch (node.nodeType) {
case 1: // Element
for (let child = node.firstChild;
child;
child = child.nextSibling) {
array = gatherText(child, array);
}
break;
case 3: // Text node
array.push(node);
break;
}
return array;
}
function fixSpaces(node) {
const texts = gatherText(node);
for (let i = 0, len = texts.length; i < len; ++i) {
const text = texts[i];
const str = text.nodeValue = text.nodeValue.replace(/ ([;?|])/g, "\u00A0$1");
if (i < len - 1 && str[str.length - 1] === " " && /^[;?!]/.test(texts[i + 1].nodeValue)) {
// This node ends with a space and the next starts with punctuation,
// replace the space with a hard space
text.nodeValue = str.substring(0, str.length - 1) + "\u00A0";
}
}
}
p {
font-size: 14px;
display: inline-block;
width: 220px;
border: 1px solid #eee;
}
<p><span>Voulez-vous coucher avec moi ce soir </span>? Non, merci.</p>
将您的问题映射到上述内容:
How and when triggering it
把它放在 script
中body
末尾的元素很早就触发它,但是在内容位于 DOM 中并因此准备好我们对其进行操作之后。
How to select the appropriate tags
我们忽略标签并在文本节点级别工作。
How to replace innerText only (ie not properties content)
通过处理文本节点级别。属性不是文本节点,因此我们不处理它们。
关于javascript - HTML : Prevent line breaks before some punctuation characters preceeded by a whitespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257080/
请参阅此示例: http://jsfiddle.net/EwtaC/1/ 图像通过display:inline-block设置样式。我知道空格会以某种方式对此产生影响..(无论出于何种原因)所以我删除
我在这里设置了一个 CSS 菜单: http://pans.saebermedia.com/ 问题是,我注意到 IE 无法正确显示菜单,现在我又开始使用装有 IE8 的计算机来修复它。我正在使用兼容模
如果你尝试这样做: var x = document.createElement("div"); x.appendChild(document.createTextNode(" te
我创建了一个报表,该报表的左侧有一个图像,该图像根据传递给该报表的参数有条件地可见。页面右侧还有另一个文本框。 我观察到,当图像的“隐藏”属性设置为“True”时,右侧的文本框将保留在正确的位置。当图
Closed. This question is off-topic。它当前不接受答案。 想要改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 已关闭
当我使用 ``# `` 在我的 Sphinx 文档中,我收到以下警告: WARNING: Inline literal start-string without end-string. 试 :samp
我正在尝试对 Linux 系统上具有 Windows 行结尾的文件应用补丁,但由于文件中的回车符而出现冲突。 -l 选项(忽略空格)不会忽略 EOL 字符。 有没有办法获得补丁来忽略Windows风格
您实际上如何更改vscode的“editor.tabSize”和“editor.insertSpaces”值?我打开了文件>首选项>用户设置,并添加了: // Place your settings
有谁知道一个命令行 exe/工具可以 可靠 从 C# 语言的行尾修剪空格? 最佳答案 它必须是命令行吗? Visual Studio 会在你做 Format Document 时( Ctrl+E+D
尝试创建一个背景色为白色的内容框。它位于页眉 div 和页脚 div 之间,它们都是图像。我无法让它与两个 div 对齐并且没有空白,只有一个或另一个。 这是 CSS: #content {
我想要对空白进行精细控制,但仍然有可读的模板。 只是想通过简单的用例看看其他人的解决方案。 {{name}} {{#if age}} , {{age}} {{/if}} # outputs {{n
如果我在Atom设置中选择Show Invisibles,则所有不可见的字符都变为可见: 我想隐藏EOL,因为它们会污染 View 。 这是可以实现的吗? 最佳答案 没有ui选项,但是可以通过Atom
我知道之前已经有人问过如何用前导空格分割字符串的问题,例如: String str = " I want to be split \t!" String[] sarr = str.split("\\
我不确定 slider 顶部和底部的额外空白来自哪里。是填充还是边距?如果有人可以帮助我,我将不胜感激。 jsfiddle.net/fH3EL 最佳答案 这是因为 bx-slider CSS 文件造成
我有一种自定义脚本语言,我正在尝试使用 XTEXT 进行语法检查。它归结为格式为单行命令 COMMAND:PARAMETERS 在大多数情况下,xtext 运行良好。我目前遇到的唯一问题是如何处理需要
我有一个包含超过 1,000,000 个条目的数据库,其中一些在值的开头/结尾包含空格字符。 我已经尝试了以下查询并且它有效,但我将不得不检查 1,000,000 条记录,因为所有 ID 都是唯一的
Perl 6 清除了其前身中的一些奇怪情况,不允许在某些地方使用空格,但在其他地方也执行工作。空间在哪里重要?有一个完整的引用会很好,因此我添加了一个社区 wiki 答案,我会用你的代表答案来更新。示
我想知道有什么方法可以在 Doxygen 的 html 的评论中插入空格吗?我在网上和 Doxygen 手册上搜索过,但找不到任何可以做到这一点的东西。 例如,我试图添加评论如下: //! mot
有人可以澄清一下空格在 Perl 6 语法中的规则中什么时候很重要吗?我通过反复试验学习了一些,但似乎无法在文档中找到实际规则。 示例 1: rule number { \d '.'? \d
我有一个代码库,由于尾随空格而使我发疯。我想把它清理干净。 我想: 删除所有尾随空格 删除文件末尾的所有换行符 将所有行尾转换为 unix (dos2unix) 将所有前导空格转换为制表符,即将 4
我是一名优秀的程序员,十分优秀!