不幸的是,根据这个 MDN 页面,没有已知的 Webkit<input>
或<textarea>
属性来禁用智能引号,但您可以使用我编写的源自此 QA 的脚本对其进行修补:如何在浏览器中禁用 textarea 字段的智能引号?
window.addEventListener('DOMContentLoaded', attachFilterToInputs );
function attachFilterToInputs() {
var textInputs = document.querySelectorAll( 'input[type=text], input[type=password], input[type=email], input[type=search], input[type=url], textarea, *[contenteditable=true]' );
for( var i = 0; i < textInputs.length; i++ ) {
textInputs[i].addEventListener( 'keypress', preventPretentiousPunctuation );
}
}
var conversionMap = createConversionMap();
function createConversionMap() {
var map = {};
// Open-quotes: http://www.fileformat.info/info/unicode/category/Pi/list.htm
map[ 0x2018 ] = '\'';
map[ 0x201B ] = '\'';
map[ 0x201C ] = '"';
map[ 0x201F ] = '"';
// Close-quotes: http://www.fileformat.info/info/unicode/category/Pf/list.htm
map[ 0x2019 ] = '\'';
map[ 0x201D ] = '\"';
// Primes: http://www.fileformat.info/info/unicode/category/Po/list.htm
map[ 0x2032 ] = '\'';
map[ 0x2033 ] = '"';
map[ 0x2035 ] = '\'';
map[ 0x2036 ] = '"';
map[ 0x2014 ] = '-'; // iOS 11 also replaces dashes with em-dash
map[ 0x2013 ] = '-'; // and "--" with en-dash
return map;
}
function preventPretentiousPunctuation( event ) {
if( event.key.length != 1 ) return;
var code = event.key.codePointAt(0);
var replacement = conversionMap[ code ];
if( replacement ) {
event.preventDefault();
document.execCommand( 'insertText', 0, replacement );
}
}