简体   繁体   English

如何使用 jQuery 更改元素的类>

[英]How can I change the class of an element with jQuery>

I have a JavaScript function that looks like this:我有一个如下所示的 JavaScript 函数:

function UpdateFilterView() {
  if (_extraFilterExists) {
    if ($('#F_ShowF').val() == 1) {
      $('#extraFilterDropDownButton').attr('class', "showhideExtra_up");
      $('#extraFilterDropDownButton').css("display", "block");
      if ($('#divCategoryFilter').css("display") == 'none') {
        $('#divCategoryFilter').show('slow');
      }
      return;
    } else {
      if ($('#divCategoryFilter').css("display") == 'block') {
        $('#divCategoryFilter').hide('slow');
      }

      $('#extraFilterDropDownButton').css("display", "block");
      $('#extraFilterDropDownButton').attr('class', "showhideExtra_down");
      return;
    }
  } else {
    if ($('#divCategoryFilter').css("display") != 'none') {
      $('#divCategoryFilter').hide('fast');
    }
    $('#extraFilterDropDownButton').css("display", "none");
  }
}

This will be triggered by the following code (from within the $(document).ready(function () {}) :这将由以下代码触发(来自$(document).ready(function () {})

$('#extraFilterDropDownButton').click(function() {
    if ($('#F_ShowF').val() == 1) {
        $('#F_ShowF').val(0);
    } else {
        $('#F_ShowF').val(1);
    }

    UpdateFilterView();
});

The HTML for this is easy:这个的 HTML 很简单:

<div id="divCategoryFilter">...</div> 
<div style="clear:both;"></div>
<div id="extraFilterDropDownButton" class="showhideExtra_down">&nbsp;</div>

I have two problems with this:我有两个问题:

  1. When the panel is hidden and we press the div button ( extraFilterDropDownButton ) the upper left part of the page will flicker and then the panel will be animated down.当面板隐藏时,我们按下 div 按钮( extraFilterDropDownButton ),页面的左上部分将闪烁,然后面板将向下动画。

  2. When the panel is shown and we press the div button the panel will hide('slow') , but the button will not change to the correct class even when we set it in the UpdateFilterView script?当显示面板并且我们按下 div 按钮时,面板将hide('slow') ,但即使我们在UpdateFilterView脚本中设置按钮也不会更改为正确的类?

The correct class will be set on the button when hovering it, this is set with the following code:将鼠标悬停在按钮上时,将在按钮上设置正确的类,这是使用以下代码设置的:

$("#extraFilterDropDownButton").hover(function() {
    if ($('#divCategoryFilter').css("display") == 'block') {
      $(this).attr('class', 'showhideExtra_up_hover');
    } else {
      $(this).attr('class', 'showhideExtra_down_hover');
    }
  },
  function() {
    if ($('#divCategoryFilter').css("display") == 'block') {
      $(this).attr('class', 'showhideExtra_up');
    } else {
      $(this).attr('class', 'showhideExtra_down');
    }
  });

To set a class completely, instead of adding one or removing one, use this:要完全设置一个类,而不是添加一个或删除一个,请使用:

$(this).attr("class","newclass");

Advantage of this is that you'll remove any class that might be set in there and reset it to how you like.这样做的好处是您将删除可能在那里设置的任何类并将其重置为您喜欢的方式。 At least this worked for me in one situation.至少这在一种情况下对我有用。

Use jQuery's使用 jQuery 的

$(this).addClass('showhideExtra_up_hover');

and

$(this).addClass('showhideExtra_down_hover');
<script>
$(document).ready(function(){
      $('button').attr('class','btn btn-primary');
}); </script>

I like to write a small plugin to make things cleaner:我喜欢写一个小插件来让事情变得更干净:

$.fn.setClass = function(classes) {
    this.attr('class', classes);
    return this;
};

That way you can simply do这样你就可以简单地做

$('button').setClass('btn btn-primary');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM