使用 CSS 或 JavaScript 关闭输入的自动更正

Turn off autocorrect of input using CSS or JavaScript

我需要使用 CSS/JavaScript 关闭自动更正。我无法在 HTML 中指定它,因为我一遍又一遍地生成多个输入,这个自动更正的红色下划线在我的情况下非常令人不安。

我想用 CSS 或 JavaScript 应用所有这些语句,但似乎不可能。

<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />

有没有一种方法可以在不使用自动更正下划线的情况下生成多个输入?

如果输入已经存在并且您无法在生成它们时设置它们的属性,那么您可以使用 querySelectorAll 然后遍历生成的节点列表来设置它们的属性,如下所示:

const inputs = document.querySelectorAll('input');

inputs.forEach(input => {
  input.setAttribute('autocomplete', 'off')
  input.setAttribute('autocorrect', 'off')
  input.setAttribute('autocapitalize', 'off')
  input.setAttribute('spellcheck', false)
})
<input />
<input />
<input />
<input />

你可以使用这样的东西 JavaScript:

inputs = container.getElementsByTagName('input');
for (i = 0; i < inputs.length; ++i) {
  inputs[i].removeAttribute('autocomplete');
}