Selectable but not editable html text field

2020-07-01 07:06发布

I am working on a mobile website and I have a text input field.

I want it to be selected and copiable but not editable. When I add readonly or onfocus="this.blur()" it becomes unselectable. How can I achieve this?

标签: html input
1条回答
男人必须洒脱
2楼-- · 2020-07-01 07:25

Check this out.

<textarea rows="10" cols="50" onclick="this.focus();this.select()" readonly="readonly">
    example text
</textarea>

Edit:

You can reassign text input value everytime it changes by adding input listener.

var inp = $("input")[0]; // select the input with proper selector
var default_value = inp.value;

inp.addEventListener("input", function () { 
    this.value = default_value;
}, false);

Working jsfiddle here.

查看更多
登录 后发表回答