如何在浏览器中禁用textarea字段的智能引号? [英] How to disable smart quotes for textarea fields in the browser?

查看:33
本文介绍了如何在浏览器中禁用textarea字段的智能引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在浏览器中运行的应用程序,用于比较字符串.使用iOS11的字符串与引号进行比较失败,因为默认情况下启用了智能引号.不能不等于不能

I have a application that runs in the browser that compares strings. String compares with quotes fail using iOS11 because it is defaulting to smart quotes being on. can’t does not equal can't

我知道可以在设置"下为整个设备禁用智能引号,但是我想在textarea级别进行处理.

I know smart quotes can be disabled under Setting for the whole device but I would like to handle it at the textarea level.

我以为我可以在按键事件中捕捉到智能报价,但iOS稍后会在幕后进行切换.

I thought I would be able to catch the smart quote on the keypress event, but iOS is making the switch later and under the covers.

textareaText是我的textarea字段中的文本
39是单引号的字符代码
8216是左侧单引号的字符代码
222是引号键的键代码
e是传递给键盘事件

textareaText is the text in my textarea field
39 is the character code for single quote
8216 is the character code for the left single smart quote
222 is the key code for the quote key
e is the event object passed to the keyboard event

keydown 
    e.keycode -> 222, e.key -> ', key.charCodeAt(0) -> 39 
    textareaText -> empty
keypress
    e.keycode -> 39, e.key -> ', key.charCodeAt(0) -> 39 
    textareaText -> empty
—> character is put in textarea here
keyup (iPad onscreen keyboard)
    e.keycode -> 222, e.key -> ', key.charCodeAt(0) -> 39 
    textareaText -> ’, textareaText.charCodeAt(0) -> 8216 
keyup (iPad external keyboard)
    e.keycode -> 0, e.key -> ', key.charCodeAt(0) -> 39 
    textareaText -> ’, textareaText.charCodeAt(0) -> 8216 

keyup事件认为字符是常规引号,而textarea包含智能引号.

The keyup event thinks the character is the regular quote while the textarea contains the smart quote.

我尝试过:

textarea.innerHTML = textarea.innerHTML.replace(/’/,'\'')

但是光标会重置为字符串的开头,我不想获取并设置光标位置.

but the cursor gets reset to the beginning of the string and I don't want to have to get and set the cursor position.

在将智能报价输入到文本区域之前,是否有任何方法可以交换它?还是在textarea级别上禁用智能引号的某种方法?还有其他解决方案吗?

Is there any way to swap the smart quote before it is entered into the textarea? Or some way to disable smart quotes at the textarea level? Is any other solution to this problem?

我最后的选择是在比较字符串之前立即替换字符串中的智能引号,但我希望浏览器显示常规引号,以使其与输入的内容,看到的内容以及被检查的内容一致.

My last resort is to replace the smart quotes in the string right before I compare it but I would prefer the browser to display the regular quotes to be consistent with what they entered, what they see, and what they are checked against.

推荐答案

最简单的方法是将 spellcheck ="false" 传递给输入元素.这基于WebKit的工作方式:https://github.com/WebKit/webkit/blob/master/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm#L4852

The simplest way is to pass spellcheck="false" to the input element. This is based on how WebKit works: https://github.com/WebKit/webkit/blob/master/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm#L4852

这篇关于如何在浏览器中禁用textarea字段的智能引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆