关闭 macOS 上的自动空格插入
当您复制然后粘贴字符串“word”时,macOS 有时会插入“word”(未更改)、“word”、“word”或“word”,具体取决于您粘贴到的内容旁边,无论它是否认为您复制了它是一个单词还是一个范围,以及您是否粘贴到书写输入(如 Notes 应用程序中的注释)或字符串输入(如 Safari 的 URL 栏)。单击并拖动以选择范围副本中的结果,双击单词副本中的结果。
在 macOS 上,Safari 和 Chrome 在所有输入中执行自动空格插入,而 Firefox 在任何输入中执行自动空格插入。 Firefox 也不遵循双击单词、右键单击单词或复制单词的本机行为。
您可以通过以下演示来体验该行为。尝试双击某个单词,然后多次粘贴,然后尝试通过拖动并多次粘贴来选择一个单词。
<div>one two three</div>
<input>
自动空格插入对于 macOS 用户来说可能很好且直观,但有时并不合适,原因与它在 Safari 的 URL 栏中不合适的原因相同:某些上下文不是英语或任何其他书面语言。在这些情况下,自动空格插入会带来令人惊讶的结果。例如,当我以 [field.{id}]
形式的微语言输入内容时遇到了这个问题,然后粘贴一个 ID,该 ID 被复制为一个单词,然后得到 [字段。 {id}]
以及它引起的错误。
理想情况下,我想指示浏览器不要将输入的值视为写入。至少,我想关闭自动空格插入和输入。我该怎么做?
我尝试过但不起作用的事情:
- 在输入和复制的文本上设置
lang=""
或lang="none"
- 设置
lang=输入和复制文本上均显示“zh”
(中文不使用单词之间的空格) - 在输入上设置
spellcheck="false"
- 设置
auto Correct="off"输入上的
(非标准,仅限 Safari)
When you copy then paste the string “word”, macOS sometimes inserts “word” (unchanged), “ word”, “word ”, or “ word ”, depending on what you’re pasting it next to, whether it thinks you copied it as a word or as a range, and whether you’re pasting into a writing input (like a note in the Notes app) or a string input (like Safari’s URL bar). Click-and-dragging to select results in a range copy, double clicking results in a word copy.
On macOS, Safari and Chrome perform automatic space insertion in all inputs, while Firefox performs it in none. Firefox also does not follow the native behavior for double clicking a word, right clicking a word, or copying a word.
You can play with the behavior with the following demo. Try double clicking a word then pasting it multiple times, then try selecting a word by dragging then pasting it multiple times.
<div>one two three</div>
<input>
Automatic space insertion is probably fine and intuitive for macOS users, but is sometimes inappropriate for the same reason it’s inappropriate in Safari’s URL bar: some contexts are not in English or any other written language. In these contexts, automatic space insertion leads to surprising results. For example, I ran into this issue when entering input in a micro-language of the form [field.{id}]
, then pasting an ID, which was copied as a word, then getting [field. {id}]
and the error that it caused.
Ideally, I want to instruct the browser not treat an input’s value as writing. Minimally, I want to turn off automatic space insertion an inputs. How can I do this?
Things I’ve tried that didn’t work:
- Setting
lang=""
orlang="none"
on both the input and the copied text - Setting
lang="zh"
on both the input and the copied text (Chinese languages do not use spaces between words) - Setting
spellcheck="false"
on the input - Setting
autocorrect="off"
on the input (non-standard, Safari only)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是取消然后模仿 JS 中的粘贴事件。
此解决方案具有以下限制:
document.execCommand
APIOne solution is to cancel then mimic the paste event in JS.
This solution has the following limitations:
document.execCommand
API