单击或悬停时更改下拉列表 CSS?

发布于 2024-07-20 15:55:57 字数 2631 浏览 3 评论 0原文

受到 https 的启发: //stackoverflow.com/questions/624102/enable-a-currently-disabled-dropdown-list-when-clicking-the-dropdown-list 我尝试了一些方法,并为禁用的选择添加了一些灰色背景。

问题是,当我单击某些选择(禁用哪个单选按钮)时,下拉列表的背景保持灰色。

即使未选中单选按钮,单击时如何将其恢复正常(白色)?

你可以在这里看到它 http://jsbin.com/okuca/

这是我的实际代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test </title>
<style type="text/css">
  label.disabled select { opacity: 0.5; filter: alpha(opacity=50); background-color:#CCC; }

</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript">
  $(function() {
    $('div.formdiv').bind('click',function() {
      $('label.disabled',this).removeClass('disabled');
      $('input:radio',this).attr('checked',true);
      $('div.formdiv').not(this).find('label').addClass('disabled').find('select').attr('selectedIndex',0);
    }).find('label').addClass('disabled');
  });
</script>
</head>

<body>

<div class="formdiv">
  <label for="Name">
    <input id="Name" name="radio1" type="radio" />Name:
    <select name="select1">
      <option value="Rose">Rose</option>
      <option value="Lily">Lily</option>
    </select>
    </br>
  </label> 
</div>

<div class="formdiv">
  <label  for="Colours">
  <input id="Colours" name="radio1" type="radio" />Colour: 
  <select name="select2">
    <option value="Red">Red</option>
    <option value="Green">Green</option>
  </select>
    </label> 
  </br>
 </div> 

<div class="formdiv">  
 <label  for="Sport">
  <input id="Sport" name="radio1" type="radio" />Sport: 
  <select name="select3">
    <option value="Tennis">Tennis</option>
    <option value="Cricket">Cricket</option>
  </select>
  </label> 
  </br>
</div>
  </body>
</html>

:可以在这里编辑: http://jsbin.com/okuca/edit

Inspired by https://stackoverflow.com/questions/624102/enable-a-currently-disabled-dropdown-list-when-clicking-the-dropdown-list I tried something and I added some grey background to disabled selection.

The problem is that when I click on some selection (which radio button was disabled) the background of the dropdown list remains grey.

How could I turn it back to normal (white) when clicked even if radio button isn't checked?

You can see it live here http://jsbin.com/okuca/

Here is my actual code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test </title>
<style type="text/css">
  label.disabled select { opacity: 0.5; filter: alpha(opacity=50); background-color:#CCC; }

</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript">
  $(function() {
    $('div.formdiv').bind('click',function() {
      $('label.disabled',this).removeClass('disabled');
      $('input:radio',this).attr('checked',true);
      $('div.formdiv').not(this).find('label').addClass('disabled').find('select').attr('selectedIndex',0);
    }).find('label').addClass('disabled');
  });
</script>
</head>

<body>

<div class="formdiv">
  <label for="Name">
    <input id="Name" name="radio1" type="radio" />Name:
    <select name="select1">
      <option value="Rose">Rose</option>
      <option value="Lily">Lily</option>
    </select>
    </br>
  </label> 
</div>

<div class="formdiv">
  <label  for="Colours">
  <input id="Colours" name="radio1" type="radio" />Colour: 
  <select name="select2">
    <option value="Red">Red</option>
    <option value="Green">Green</option>
  </select>
    </label> 
  </br>
 </div> 

<div class="formdiv">  
 <label  for="Sport">
  <input id="Sport" name="radio1" type="radio" />Sport: 
  <select name="select3">
    <option value="Tennis">Tennis</option>
    <option value="Cricket">Cricket</option>
  </select>
  </label> 
  </br>
</div>
  </body>
</html>

You can edit it here:
http://jsbin.com/okuca/edit

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

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

发布评论

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

评论(1

问题是,在松开鼠标按钮(并且下拉菜单消失)之前,单击处理程序不会被触发,因此仍然应用 label.disabled select 样式。

有两种方法可以解决这个问题。 首先,您可以为 :focus 添加另一个 CSS 规则,它会覆盖禁用的样式:

label.disabled select:focus { opacity: 1.0; filter: alpha(opacity=100); background-color:white; }

但是,如果您的样式变得更加复杂,这可能会给您带来麻烦。 相反,我建议将您的点击处理程序更改为 mousedown 处理程序:

$('div.formdiv').bind('mousedown',function() {

这会导致处理程序在下拉列表出现之前触发(并且类将被删除)。

The problem is that the click handler isn't being fired until you let go of the mouse button (and the drop-down vanishes), so the label.disabled select style is still applied.

There are two ways to fix this. First, you could add another CSS rule for :focus, which overrides the disabled styling:

label.disabled select:focus { opacity: 1.0; filter: alpha(opacity=100); background-color:white; }

However, this could get you into trouble if your styles get more convoluted. Instead, I'd recommend changing your click handler to a mousedown handler:

$('div.formdiv').bind('mousedown',function() {

This causes the handler to fire (and the class to be removed) before the drop-down appears.

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