是否可以通过 JavaScript 禁用文本区域中的拼写检查?

发布于 2024-10-07 08:59:41 字数 570 浏览 0 评论 0原文

是否可以通过 JavaScript 禁用文本区域中的拼写检查?

我一直在查看 HTML5“拼写检查”属性,我可以让它独立工作。但是,我希望能够通过 JavaScript 更改此属性。

阅读上述链接提供的文档后,我将以下几行代码放在一起(来自较大文件的片段;我知道它们无法单独工作):

document.querySelector("#editor").spellcheck = "false";

并且

document.querySelector("#editor").spellcheck = "true";

但它似乎不起作用。我是否犯了错误或误解了文档? (毫无帮助的是,Google Chrome 中的 JavaScript 控制台没有返回错误。)或者还有其他方法可以做到这一点吗?

Is it possible to disable spellcheck in a textarea via JavaScript?

I've been looking at the HTML5 'spellcheck' attribute, and I can get it to work on its own. However, I'd like to be able to change this attribute via JavaScript.

After reading the documentation provided at the above link, I put together the following lines of code (snippets from a larger file; I know they won't work on their own):

document.querySelector("#editor").spellcheck = "false";

and

document.querySelector("#editor").spellcheck = "true";

But it doesn't seem to work. Have I made a mistake or misunderstood the documentation? (Unhelpfully, the JavaScript console in Google Chrome is not returning an error.) Or is there any other way to go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶茶白久 2024-10-14 08:59:41

您将其设置为 String "true" (这是一个 true 值)和 String < code>"false" (这也是一个 true 值)。

使用布尔值,而不是字符串。去掉引号。

You are setting it to the String "true" (which is a true value) and to the String "false" (which is also a true value).

Use Booleans, not Strings. Get rid of the quotes.

眉黛浅 2024-10-14 08:59:41

至少对我来说,字符串/布尔值的区别不会影响 JavaScript 中的 HTML 属性 - JavaScript 解释 "false" == false

您可能会遇到拼写检查未取消的问题,因为您没有强制它重新评估文本区域。要强制执行此操作,请尝试以下代码:

var editor = document.getElementById("editor");
editor.spellcheck = false;
editor.focus();
editor.blur();

焦点强制浏览器重新评估文本区域,在本例中没有拼写检查。希望有帮助!

At least for me, the String/Boolean distinction doesn't affect HTML attributes in JavaScript—Javascript interprets "false" == false.

You may be running into issues with the spellcheck not being canceled because you're not forcing it to re-evaluate the textarea. To force this, try this code:

var editor = document.getElementById("editor");
editor.spellcheck = false;
editor.focus();
editor.blur();

The focus forces the browser to re-evaluate the textarea, in this case with no spellcheck. Hope that helps!

阳光①夏 2024-10-14 08:59:41

假设它是一个基本属性。

以下内容应该在没有库的情况下工作:

document.getElementById('editor').setAttribute('spellcheck', 'true'); // or false

并且使用jquery:

$('#editor').attr('spellcheck', 'true'); // or false

Assuming it's a basic attribute.

The following should work without library:

document.getElementById('editor').setAttribute('spellcheck', 'true'); // or false

and with jquery:

$('#editor').attr('spellcheck', 'true'); // or false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文