通过应用自定义css类来禁用html输入元素 [英] Disable html input element by applying custom css class

查看:134
本文介绍了通过应用自定义css类来禁用html输入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过应用我的自定义css类来禁用div的所有输入元素。
但我找不到任何可以禁用输入元素的CSS属性。
目前我在做什么

I want to disable my all input element of a div by applying my custom css class. but i could not find any css attribute which can disable an input element. currently what am i doing

  $('#div_sercvice_detail :input').attr('disabled', true);
 $('#retention_interval_div :input').addClass("disabled"); 

它可以禁用div的所有输入元素与css attr但我想应用我的自定义类为禁用所有输入都有一些额外的css属性

which can disable all input element of div with css attr but i want to apply my custom class for disable all input with some extra css attributes

 $('#retention_interval_div :input').addClass("disabled");

.disabled{
color : darkGray;
font-style: italic;
/*property for disable input element like*/
/*disabled:true; */
}    

任何建议,使用jquery不使用.attr ',true);?

any suggestion for doing this with jquery without using .attr('disabled', true);?

推荐答案

没有办法使用CSS禁用一个元素,将应用于已禁用的元素:

There is no way to disable an element with just CSS, but you can create a style that will be applied to disabled elements:

<style>
#retention_interval_div​​ input[type="text"]:disabled { 
    color : darkGray;
    font-style: italic;
}​
</style>

然后在你的代码中你只需要说:

Then in your code you just have to say:

 $('#retention_interval_div :input').prop("disabled", true);

演示: http://jsfiddle.net/nnnnnn/DhgMq/

(当然,:disabled 旧浏览器不支持CSS选择器。)

(Of course, the :disabled CSS selector isn't supported in old browsers.)

请注意,如果您使用jQuery版本> = 1.6,您应该使用 .prop ()而不是 .attr()更改禁用状态。

Note that if you're using jQuery version >= 1.6 you should use .prop() instead of .attr() to change the disabled state.

你显示的代码不是禁用它应用该类的相同的元素 - 选择器是不同的。如果这只是一个拼写错误,那么你可以简化它一行:

The code you showed is not disabling the same elements that it applies the class to - the selectors are different. If that's just a typo then you can simplify it to one line:

$('#retention_interval_div :input').addClass("disabled").attr('disabled', true);

这篇关于通过应用自定义css类来禁用html输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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