-2

jQueryを使用してドロップダウンリストからボディクラスを変更するにはどうすればよいですか?

4

3 に答える 3

1
$('select').change(function() {
     $('body').addClass($(this).val());
});

「jQuery 入力変更」でググると.change()方法が見つかります。「jQuery change class」をグーグルで検索すると、クラスを追加、削除、および変更できる複数のメソッドに出くわします。

少し創造性を加えると、アインシュタインの能力をはるかに超えるものを魔法のように発明したことになります。

Google は開発者にとって最も価値のあるツールです。大きな力には大きな責任が伴います。

于 2012-11-23T15:03:55.057 に答える
1
$(document).ready(function(){
  // On load

  $('#MyDropDownId').change(function(){ // on change event
    $('body')
    .removeClass() // remove the classes on the body
     // or removeClass('class1 class2 ...') in order to not affect others classes
    .addClass( $(this).val() ); // set the new class
  })

})

仮定:

<select id="MyDropDownId">
  <option value="class1">First Class</option>
  <option value="class2">2nd Class</option>
</select>

JQuery の変更の詳細を読む

于 2012-11-23T15:04:19.167 に答える
0

あなたがしたいと仮定するとchange the body class on change event of dropdown

ライブデモ

<select id="dropdownId" >
  <option>class1</option>
  <option>class2</option>
  <option>class3</option>    
</select>

$('#dropdownId').change(function() {
    $('body').attr('class', $(this).val());
});
于 2012-11-23T15:05:29.157 に答える