admin管理员组文章数量:1400384
How can I show a blinking cursor (allowing for text selection via keyboard) in a div but keep it read-only (disallowing text input)?
I know I can set contentEditable
to enable the cursor on a div but then users can edit the contents. I've tried adding both contentEditable
and readonly
to the div but it seems readonly
is only effective on input elements, like textarea or input.
I also have tried using a textarea and setting it to readonly so it shows the cursor but doesn't allow text input, like this:
<textarea readonly>Go ahead and move the cursor here but don't try to add text</textarea>
How can I show a blinking cursor (allowing for text selection via keyboard) in a div but keep it read-only (disallowing text input)?
I know I can set contentEditable
to enable the cursor on a div but then users can edit the contents. I've tried adding both contentEditable
and readonly
to the div but it seems readonly
is only effective on input elements, like textarea or input.
I also have tried using a textarea and setting it to readonly so it shows the cursor but doesn't allow text input, like this:
<textarea readonly>Go ahead and move the cursor here but don't try to add text</textarea>
This is the functionality I'm looking for but I want to do this with a div or some other non-input element. I'm open to using 3rd party libraries.
Note: "Cursor" or "caret" here is referring to the blinking lines that indicates where text selection starts/ends.
Share Improve this question edited Feb 15, 2019 at 13:31 Brady Dowling asked Feb 15, 2019 at 12:35 Brady DowlingBrady Dowling 5,5724 gold badges41 silver badges67 bronze badges 02 Answers
Reset to default 7Here you go :) . All you need to do is disable cut/copy/paste and key press events.
<div
contenteditable="true"
oncut="return false"
onpaste="return false"
onkeydown="return false;"
style="user-drag: none;"
ondragenter="return false;"
ondragleave="return false;"
ondragover="return false;"
ondrop="return false;">
Inner content
</div>
In Vanilla JavaScript, This is the simplest example of how you can go about doing it. I am using a class here, but as other answers showed, you could change it to an actual attribute or whatever suits your linking.
const uneditables = Array.from(
document.querySelectorAll(".editable-but-not-really")
);
const doNothing = e => e.preventDefault();
uneditables.forEach(element => {
element.setAttribute("contentEditable", true);
element.addEventListener("oncut", doNothing, false);
element.addEventListener("onpaste", doNothing, false);
element.addEventListener("keydown", doNothing, false);
});
<div class="editable-but-not-really">I am here to stay</div>
<div class="editable-but-not-really">You cannot edit me</div>
<div class="editable-but-not-really">I was born to stay</div>
<div class="editable-but-not-really">As I am, and I don't</div>
<div class="editable-but-not-really">want anything to do with</div>
<div class="editable-but-not-really">your stinky pointer</div>
本文标签: javascriptHow to show blinking text cursorcaret in a readonly divStack Overflow
版权声明:本文标题:javascript - How to show blinking text cursorcaret in a read-only div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744165448a2593530.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论