admin管理员组文章数量:1400863
I have an conteneditable with predefined text inside:
div{
padding: 10px;
border: 1px solid black;
}
.some-class{
background: yellow;
padding: 0px 5px;
}
<div contenteditable=true>Hello <span class="some-class">Dont delete me!</span> people!</div>
I have an conteneditable with predefined text inside:
div{
padding: 10px;
border: 1px solid black;
}
.some-class{
background: yellow;
padding: 0px 5px;
}
<div contenteditable=true>Hello <span class="some-class">Dont delete me!</span> people!</div>
I want to prevent users from deleting <span class="some-class">Dont delete me!</span>
from it.
I guess I should check what text is being deleted when user press back space on onChange event. But I don't know how to check if deleted content is just on letter like H
or the whole div <span class="some-class">Dont delete me!</span>
- Can I ask why you have content you don't want edited in a content editable element? – Craicerjack Commented May 10, 2017 at 15:14
- 1 Please don't link to external code when you can easily add it here. – Scott Marcus Commented May 10, 2017 at 15:14
- @Craicerjack this is very specific case, where user can add some predefined values that he/she shouldn't be able to deleted latter in process. – sir_K Commented May 10, 2017 at 15:48
1 Answer
Reset to default 9Instead of assigning contenteditable=true
to the entire container, break down the contents into separate span elements and assign it there like so:
div{
padding: 10px;
border: 1px solid black;
}
.some-class{
background: yellow;
padding: 0px 5px;
}
<div><span contenteditable=true>Hello </span><span class="some-class" contenteditable=false>Dont delete me!</span><span contenteditable=true> people!</span></div>
Edit: If you don't want to change the markup, you can use jQuery to search for the string of text that isn't supposed to be deleted and then make sure it persists any attempts to remove it.
$('#div').on('input', function() {
var span = $('.some-class').text();
if (span.indexOf('Dont delete me!') < 0) {
$('.some-class').text('Dont delete me!');
}
});
div {
padding: 10px;
border: 1px solid black;
}
.some-class {
background: yellow;
padding: 0px 5px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div" contenteditable=true>Hello <span class="some-class">Dont delete me!</span> people!</div>
本文标签: javascriptPrevent user from deleting specific text from inputStack Overflow
版权声明:本文标题:javascript - Prevent user from deleting specific text from input - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744247612a2597095.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论