我正在使用 JQuery UI 和 Bootstrap 所以我遇到了这个问题,我认为这是两者之间的冲突,因为在正常情况下是 textarea 或 input 文件本质上是可编辑的,但我在测试了上述所有答案后提出了这个解决方案,但没有一个解决对所有主要浏览器的跨浏览器支持,但我解决了它,我喜欢分享我的解决方案,你可以使用它输入文本和文本区域
(在桌面上测试:IE(所有版本)、Chrome、Safari、Windows Edge、Firefox、Windows 上的 Visual Studio Cordova Ripple Viewer 和 Visual Studio Cordova Windows 10 应用商店应用程序)
(在移动设备上测试:Chrome、Firefox、Android Internet 浏览器和 Android 上的 Visual Studio Cordova 应用程序和 Visual Studio Cordova Windows 8 + 8.1 + 10 手机应用程序)
这是 HTML 代码:
<textarea contenteditable id="textarea"></textarea>
这是 CSS 代码:
textarea {
-webkit-user-select: text !important;
-khtml-user-select: text !important;
-moz-user-select: text !important;
-ms-user-select: text !important;
user-select: text !important;
/*to make sure that background color and text color is not the same (from the answers above)*/
background-color:#fff !important;
color:#733E27 !important;
}
这是文档就绪的 JQuery 代码
$("textarea").click(function() {
setTimeout(function(){
$("textarea").focus();
//add this if you are using JQuery UI (From The Solutions Above)
$("textarea").enableSelection();
var val = $("textarea").val();
if (val.charAt(val.length-1) !== " " && val.length !== 1) {
alert(val.length);
val += " ";
}
$("textarea").val(val);
}, 0);
});
if (navigator.userAgent.indexOf('Safari') !== -1 || navigator.userAgent.indexOf('Chrome') !== -1) {
//alert('Its Safari or chrome');
$("textarea").onfocus(function(e) {
setTimeout(function(){
var end;
if ($("textarea").val === "") {
end = 0;
} else {
end = $("textarea").val.length;
}
if ($("textarea").setSelectionRange) {
var range = document.getElementById('textarea').createTextRange();
if (range) {
setTimeout(range, 0, [end, end]);
} else { // IE style
var aRange = document.getElementById('textarea').createTextRange();
aRange.collapse(true);
aRange.moveEnd('character', end);
aRange.moveStart('character', end);
aRange.select();
}
}
e.preventDefault();
return false;
}, 0);
});
}
你可以在我的网络应用程序上测试它www.gahwehsada.com