Over-ride Chrome Browser spell check language using JQuery or JavaScript

纵然是瞬间 提交于 2021-02-07 03:08:15

问题


I am using the latest Chrome browser Version 55.0.2883.87 m.

On input elements on a HTML web page, how do I dynamically over-ride the language of the Chrome browser spell checker using JQuery/JavaScript?

For example if the page is displayed in English (US) and the spell checker is set to English (US), how can I pass in the German language code of de, so that the text area of all the input elements inside the form will spell check the input(s) with the German language and not the English (US) language?

Here is an example HTML text area.

<textarea cols="40" id="id_objective_details" maxlength="1000" name="objective_details" rows="10" class="kmw-disabled keymanweb-font textArea01"></textarea>

回答1:


tl;dr

You can change the language using setAttribute('lang', lang) but Chrome doesn't use the lang attribute for spellcheck. This is a won't fix issue. Check the thread for more info.


Instead Chrome makes use of the user dictionaries which has to be selected by the user. (Try right-clicking a text-area -> Spellcheck -> Select language). Also the user should have the dictionary installed or else the spellcheck will not be possible.

You can try the following script and note that changing the lang attribute will not have effect but changing the language manually as mentioned above will.

function toLang(lang) {
  document.getElementById('id_objective_details').setAttribute('lang', lang);
}
<h3>Here is some random German text</h3>
<p>Hallo Welt, hallo und hallo wieder Welt.</p>

<h3>Here is some random English text</h3>
<p>Hello world, hello and hi again world.</p>
<textarea cols="40" id="id_objective_details" maxlength="1000" name="objective_details" rows="10" class="kmw-disabled keymanweb-font textArea01" spellcheck="true"></textarea>
<br>
<button onclick="toLang('en')">English check</button>
<button onclick="toLang('de')">German check</button>

You can perhaps use some external libraries like JavaScript SpellCheck or JQuery SpellCheck to get the job done.



来源:https://stackoverflow.com/questions/41252737/over-ride-chrome-browser-spell-check-language-using-jquery-or-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!