gpt4 book ai didi

javascript - 纯文本和单行 contenteditable div

转载 作者:搜寻专家 更新时间:2023-10-31 22:42:35 25 4
gpt4 key购买 nike

我正在尝试制作“单行”和“纯文本”contenteditable分区换句话说,我只想制作一个 <input type="text"> , 但不以这种形式出现。如何使用 contenteditable 的形式div呈现 <input type="text"> 的形式?

最佳答案

您可以使用 JavaScript 为 contentEditable 元素禁用 Enter 键。这是一个 jQuery 版本:

$('[contenteditable]').keypress(function (e) {
if (e.keyCode == 10 || e.keyCode == 13) {
e.preventDefault();
// Submit the form, etc.
}
})
.on('paste keyup', function () {
var _this = this;
setTimeout(function () {
// remove the markups
$(_this).html($(_this).text());
}, 0);
});
[contenteditable] * {
display: none; /* hide all descendants */
}

[contenteditable] {
display: inline-block;
width: 200px;
overflow: hidden;

-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;

white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div contenteditable>
blah blah blah...
</div>

关于javascript - 纯文本和单行 contenteditable div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30254675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com