【问题标题】:Div css change when select option is selected with jQuery使用 jQuery 选择选择选项时,div css 更改
【发布时间】:2012-02-19 22:10:39
【问题描述】:

我已经接近答案了,但我在 jQuery 方面还不够好,无法对其进行调整以达到我想要的方式。

I need to change the css class of a div when a select option is selected.
所以如果代码是:

<style>
  .red{
  background-color:red;
}
  .green{
  background-color:green;
}
  .blue{
  background-color:blue;
}
</style>

<html>

<div id="box"> </div>

<select>
  <option value=1>Red</option>
  <option value=2>Green</option>
  <option value=3>Blue</option>
</select>

</html>

在选择适当的选项时,我将如何更改“#box”的背景颜色?

THIS 如此接近。 (Found here)

【问题讨论】:

    标签: jquery css select background option


    【解决方案1】:
    $('select').change(function(){
        $('#box').removeClass('red green blue').addClass(
            $(this).find('option:selected').text().toLowerCase()
        );
    })
    .change();
    

    这是小提琴:http://jsfiddle.net/aHYKG/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2023-03-08
      相关资源
      最近更新 更多