是否可以通过 CSS 将输入字段设为只读? [英] Is it possible to make input fields read-only through CSS?

查看:27
本文介绍了是否可以通过 CSS 将输入字段设为只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过应用 readonly 布尔属性将输入元素设为只读,并且作为一个不受 CSS 影响的属性.

I know that input elements are made read-only by applying the readonly boolean attribute, and being an attribute it is not affected by CSS.

另一方面,我的场景似乎非常适合 CSS,所以我希望有某种 CSS 技巧可以让我做到这一点.我的表单上有一个可打印版本超链接.单击它会显示文档的 ... 可打印版本.它主要是 CSS 的东西,我的 print.css 看起来像这样:

On the other hand, my scenario seems to be a very good fit for CSS, so I was hoping there is some kind of a CSS trick to let me do it. I have a printable version hyperlink on my form. Clicking it displays a ... printable version of the document. It is mostly CSS stuff, my print.css looks like this:

html.print {
    width: 8.57in;
}

.print body {
    font: 9pt/1.5 Arial, sans-serif;
    margin: 0 1in;
    overflow: auto;
}

.print #header, .print #footer {
    display: none;
}

.print .content {
    background-color: white;
    overflow: auto;
}

.print .fieldset > div.legend:first-child {
    background: white;
}

.print ::-webkit-input-placeholder {
    /* WebKit browsers */
    color: transparent;
}

.print :-moz-placeholder {
    /* Mozilla Firefox 4 to 18 */
    color: transparent;
}

.print ::-moz-placeholder {
    /* Mozilla Firefox 19+ */
    color: transparent;
}

.print :-ms-input-placeholder {
    /* Internet Explorer 10+ */
    color: transparent;
}

.print .check-mark {
    display: inline;
}

.print input[type=checkbox] {
    display: none;
}

.print .boolean-false {
    display: none;
}

还有一些javascript片段,例如:

There are also a few javascript pieces, such as:

  • print 类添加到 html 元素
  • 显示没有滚动条的表格
  • 其他一些小事,例如隐藏任何弹出式覆盖.
  • Adding the print class to the html element
  • Displaying tables without scroll bars
  • A few other minor things, like hiding any popup overlays.

我目前的问题是输入字段.它们应该是只读的,但是,我不知道如何以最少的代码更改来做到这一点.CSS 可能是一个完美的解决方案.

My current problem is input fields. They should be read-only, however, I have no idea how to do it with minimum changes to the code. CSS could be a perfect solution.

有什么想法吗?

推荐答案

仅使用 CSS?通过使用 user-select:none,这在文本输入上是可能的:

With CSS only? This is sort of possible on text inputs by using user-select:none:

.print {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;          
}

JSFiddle 示例.

值得注意的是,这在不支持 CSS3 或不支持 用户选择属性.理想情况下,readonly 属性应该提供给您希望设为只读的输入标记,但这确实可以作为一种 hacky CSS 替代方案.

It's well worth noting that this will not work in browsers which do not support CSS3 or support the user-select property. The readonly property should be ideally given to the input markup you wish to be made readonly, but this does work as a hacky CSS alternative.

使用 JavaScript:

With JavaScript:

document.getElementById("myReadonlyInput").setAttribute("readonly", "true");

CSS 方法不再适用于 Chrome (29).-webkit-user-select 属性现在似乎在输入元素上被忽略了.

The CSS method no longer works in Chrome (29). The -webkit-user-select property now appears to be ignored on input elements.

这篇关于是否可以通过 CSS 将输入字段设为只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆