简体   繁体   English

跨浏览器:禁用输入字段的不同行为(文本可以/不能被复制)

[英]Cross-Browser: Different behaviors for disabled input fields (the text can/can't be copied)

I have a input html field that is disabled. 我有一个禁用的输入html字段。 In some browsers (Chrome, Edge, Internet Explorer and Opera) is possible to select and copy the text but, at least, in Firefox it is not possible. 在某些浏览器(Chrome,Edge,Internet Explorer和Opera)中可以选择和复制文本,但至少在Firefox中是不可能的。

You can test it by executing the following code in different browsers: 您可以通过在不同的浏览器中执行以下代码来测试它:

 <input type="text" disabled value="is disable"> <input type="text" readonly value="is readonly"> 

I read in here that disabled fields can be focus , here that A disabled input element is unusable and un-clickable and in here that A read-only input field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it) . 我在这里读到禁用的字段可以是焦点这里 禁用的输入元素是不可用和不可点击的,并且在这里 不能修改只读输入字段(但是,用户可以对其进行选项卡,突出显示它,以及从中复制文本)

I didn't find anything saying that the text from disabled inputs can't be copied. 我没有发现任何说禁用输入的文本无法复制的内容。

There is standard behavior and some browsers are not respecting it or can the browsers choose if the text from a disabled input can or can't be copied? 有标准的行为,有些浏览器不尊重它,或者浏览器可以选择是否可以复制来自禁用输入的文本?

And there is a solution for allowing, in all browsers, the text from a disabled input field to be copied? 并且有一个解决方案允许在所有浏览器中复制来自禁用输入字段的文本?

Note: My application is implemented using Angular/TypeScript languages. 注意:我的应用程序是使用Angular / TypeScript语言实现的。


It seams that the only browser that has a distinct behavior is firefox. 它认为唯一具有不同行为的浏览器是firefox。 I opened an issue in here trying to understand if is a design option or if is a bug. 我在这里打开了一个问题,试图了解它是否是一个设计选项或者是否是一个bug。 I'm waiting now for an answer. 我现在在等一个答案。

Change your field from disabled to readonly . 将您的字段从disabled更改为readonly That is the correct attribute for the behaviour you want. 这是您想要的行为的正确属性。

Don't waste time hacking a javascript solution together as it will be even more flaky than the minor differences in browser behaviour. 不要浪费时间一起破解javascript解决方案,因为它会比浏览器行为的细微差别更加不稳定。

If the issue is that you want your readonly fields to look like disabled fields, it's fairly easy to style an input with a readonly attribute set. 如果问题是您希望您的只读字段看起来像禁用字段,那么使用readonly属性集设置输入样式相当容易。 You don't need to change behaviour to change appearance. 您无需更改行为即可更改外观。

 input[readonly] { background: #EEE; color: #666; border: solid 1px #CCC; } 
 <input readonly value="I am readonly"> 

There is nothing wrong with doing this when you disable a form control. 禁用表单控件时执行此操作没有任何问题。

<input type="text" disabled readonly value="is disable">

or 要么

<input type="text" disabled="disabled" readonly="readonly" value="is disable">

However, that may not produce consistent behavior as it pertains to copying text (after selecting it). 但是,这可能不会产生与复制文本相关的一致行为(在选择之后)。

An Imperfect JavaScript Way: 一种不完美的JavaScript方式:

I have not used a select type event in a while, but I suggest toggling the ability to select the text. 我有一段时间没有使用过select类型的事件,但我建议切换选择文本的能力。 You might also play with the focus and blur events. 您也可以使用focusblur事件。

External CSS Style Sheet: 外部CSS样式表:

.preventSelection {user-select: none}  // IE 10+

W3Schools: user-select: W3Schools:用户选择:

Quick and Dirty Event Handler: 快速和脏事件处理程序:

function preventDisabledControlTextCopy(e)
{
    // blah, blah, blah

    if (e.target.disabled === true) {
        // Add "preventSelection" to e.target.className
        // Alternatively, change the focus to somewhere else!
    } else {
        // Remove "preventSelection" from e.target.className
    }
}

// Blah, blah, blah-blah blah

textBox.addEventListener("select", preventDisabledControlTextCopy, false);

It is never a waste of time to seek options. 寻求选择绝不浪费时间。 I skipped many details, but I made the important part explicit (as Stackoverflow is a tool people use to learn things). 我跳过了许多细节,但我明确了重要部分(因为Stackoverflow是人们用来学习东西的工具)。 Implementation is up to you. 实施取决于您。

Final Thoughts: 最后的想法:

The best answer might be to use CSS and JavaScript and toggle visibility: hidden (IE 4+) or display: none (IE 8+), depending on your scenario, DOM structure, and the complexity you are able to manage. 最佳答案可能是使用CSS和JavaScript并切换visibility: hidden (IE 4+)或display: none (IE 8+),具体取决于您的场景,DOM结构以及您能够管理的复杂性。

Dynamic forms are a great way to experience the interplay between HTML, CSS, and JavaScript. 动态表单是体验HTML,CSS和JavaScript之间相互作用的好方法。

I tried to use user-select in an input but it can't prevent selecting of text to it. 我试图在输入中使用user-select ,但它无法阻止选择文本。 Even wrap it to a div with a user-select: none style still not work. 甚至用user-select: none将它包装到div user-select: none样式仍然不起作用。 It's only work for (I think) non-focusable element like div, span, etc. 它只适用于(我认为)不可聚焦的元素,如div,span等。

However, right now I think user-select: none is the only better option if you want to ensure that user won't copy the text from the page even in many different browsers (check user-select browsers compatibility). 但是,现在我觉得user-select: none是唯一更好的选择,如果你想确保即使在许多不同的浏览器中用户也不会从页面复制文本(检查用户选择的浏览器兼容性)。 So I would suggest, create a component that something like this: 所以我建议,创建一个像这样的组件:

<input  *ngIf="!isDisabled" value="model" />
<div *ngIf="isDisabled" style="user-select: none">{{model}}</div>

Caveat: You have to styled the div to be more like a disabled input. 警告:您必须将div设置为更像残疾人输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM