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

时间:2018-10-13 20:48:38

标签: javascript css autocorrect

我需要使用CSS / JavaScript关闭自动更正。我无法在HTML中指定它,因为我会一遍又一遍地产生多个输入,而这种自动更正的红色下划线对我来说很不方便。

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

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

有没有办法生成多个输入而无需自动更正下划线?

2 个答案:

答案 0 :(得分:1)

如果输入已经存在,并且您无法在生成输入时设置它们的属性,那么可以使用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 />

答案 1 :(得分:1)

您可以使用JS使用类似的方法。为什么没有自定义属性就无法生成它们?那将是最好的方法。

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