jQuery hack 在 Opera 中失败,有替代方案吗?

发布于 2024-10-08 08:04:39 字数 489 浏览 0 评论 0原文

我有一个表单,我想将其用作报告视图和表单。最初,文本输入(文本和文本区域)的样式设置为没有边框/背景。我还使用以下 jQuery 代码来确保它们会拒绝焦点:

$('input[type="text"], textarea').focus(function() {
  $(this).blur();
});

这在 Firefox 中有效,但在 Opera 中无效(也许还有其他 webkit 浏览器)。我不想使用 disabled 属性,因为这样我就无法将输入样式设置为不引人注目(一般意义上)。我可以隐藏输入并在状态为 view 的位置显示一个 span 或 div,然后在状态更改为 form 时交换可见隐藏属性,事实上我我正在为选择、单选按钮和复选框执行此操作。但如果我能让它按原样工作的话,工作量就会减少。有什么建议吗?

编辑 - 删除了 webkit 标签,谢谢 Fylke。

I have a form which I want to use both as a report view and a form. Initially the text inputs (text and textarea) are styled to have no border / background. I also use the following jQuery code to make sure they will reject focus:

$('input[type="text"], textarea').focus(function() {
  $(this).blur();
});

This works in Firefox, but it doesn't in Opera (and maybe other webkit browsers). I don't want to use disabled property because then I cannot style the input to be unobtrusive (in the general sense). I can hide the input and display a span or div in its place where the state is view and then swap the visible-hidden property when state changes to form, in fact I'm doing this for selects, radio buttons, and checkboxes. But it'd be less work if I can get it work as is. Any suggestions?

Edit - Removed webkit tag, thank you Fylke.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2024-10-15 08:04:39

为什么不直接使用 “readonly”属性 而不是“禁用”?将“readonly”设置为 true 不会改变字段的外观(意味着您可以使用 CSS 做到这一点),但它会阻止修改。

也就是说,当字段值占用的空间多于布局上分配的空间时,这种重用布局的方法可能会出现问题。当它们是可编辑文本字段时,用户通常知道他们可以滚动值,但当字段看起来不像字段时,这就变得很奇怪。

Why not just use the "readonly" attribute instead of "disabled"? Setting "readonly" to true will not change the way the fields look (meaning that you can do that with CSS), but it prevents modification.

That said, this approach to re-using layout can be problematic when your field values take up more room than allocated on the layout. When they're editable text fields, users generally understand that they can scroll the value around, but that becomes pretty weird when the fields don't look like fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文