在这上的addClass('active')似乎无法正常工作

时间:2019-07-19 10:40:05

标签: jquery html css

$(this).addClass('active')不起作用

我陷入了试图添加活动类的某些代码中,但是代码似乎无法正常工作。

我尝试了几种方法,但无法解决如果在CSS末尾添加!important但我不喜欢使用它,效果很好。

这是我第一次编写jquery,任何其他代码改进建议也将有所帮助。

var obj = [{
    firstName: 'Chakradhari',
    lastName: 'Jamili',
    errorProne: true,
  },
  {
    firstName: 'Nikitha',
    lastName: 'Mandadi',
    errorProne: false,
  },
  {
    firstName: 'Jyothi',
    lastName: 'Madala',
    errorProne: true,
  },
  {
    firstName: 'Mahesh',
    lastName: 'Pola',
    errorProne: false,
  },
  {
    firstName: 'Maheswari',
    lastName: 'Poli',
    errorProne: false,
  },
  {
    firstName: 'Maheswari',
    lastName: 'Poli',
    errorProne: false,
  }
];

$(document).ready(function() {

  var view = $('#todaysFlow');
  var move = '100px';

  if (obj.length > 5) {
    $(".arrow").css('display', 'block');
    $(".right").click(function() {
      view
        .stop(false, true)
        .animate({
          left: '-=' + move
        }, {
          duration: 400
        })
    });

    $(".left").click(function() {
      console.log(parseInt(view.css('left')))
      if (parseInt(view.css('left')) <= 0) {
        view
          .stop(false, true)
          .animate({
            left: '+=' + move
          }, {
            duration: 400
          })
      }

    });
  }


  obj.forEach(function(obj, i) {
    var div = $('<div>')
    var p = $('<p>')
    var errorSpan = $('<span>');
    var elems = $('.todaysFlow div')

    div.click(function() {
        $('#displayContent').html(obj.firstName[0] + ' ' + obj.lastName[0]);
        elems.not(this).removeClass('active');
        $('.error_exclamation').remove();

        /*
         This active class doesn't seem to work fine even though color is being added 				 but border is getting striken away I am guessing it has something to do 							 with presidence.
        */
        $(this).addClass('active');
        if (obj.errorProne) {
          errorSpan.addClass('error_exclamation');
          $(this).append(errorSpan);
        }
      })
      .addClass('generate_div_css');

    p.addClass('generate_div_p_css')
      .html(obj.firstName[0] + ' ' + obj.lastName[0]);
    div.append(p)
    $('#todaysFlow').append(div);

    if (i == 0) {
      div.click();
    }
  })
})
.mainDiv {
  width: 500px;
  position: relative;
  height: 80px;
  background-color: #ccc;
}


/* This is the active class is being used which isn't affective */

.active {
  color: blue;
  border: 1px solid blue;
}

.arrow {
  width: 40px;
  height: 40px;
  position: absolute;
  background-color: #ccc;
  display: none;
}

.left {
  left: 0
}

.left::before {
  content: '\227A'
}

.right {
  right: 0
}

.right::after {
  content: '\227B'
}

.peopleInfo {
  width: 410px;
  position: absolute;
  left: 40px;
  overflow: hidden;
  height: 100%;
}

.todaysFlow {
  width: 1200px;
  padding: 5px;
  position: relative;
  left: 0;
}


/* This is the css I have used for styling the div's */

.generate_div_css {
  position: relative;
  width: 70px;
  height: 70px;
  border: 1px solid #000;
  border-radius: 50%;
  display: inline-block;
  margin-right: 10px;
}

.generate_div_p_css {
  position: absolute;
  top: 13%;
  left: 34%;
  margin-right: -34%;
}

.random {
  position: relative;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 1px solid black;
}

.error_exclamation {
  position: absolute;
  background: orange;
  right: 0;
  padding: 5px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  color: white;
}

.error_exclamation::before {
  content: '1'
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="mainDiv">
  <div class="arrow left"></div>
  <div class="peopleInfo">
    <div id="todaysFlow" class="todaysFlow"></div>
  </div>
  <div class="arrow right"></div>
</div>
<div id="displayContent"></div>

https://jsfiddle.net/Chakradhari/ecm3foxr/114/

请帮助我改善代码。谢谢

1 个答案:

答案 0 :(得分:0)

addClass()调用在您的代码中运行良好-从div的文本颜色变为蓝色这一事实可以看出。

问题是因为border类中的.active样式没有足够高的优先级来覆盖.generate_div_css中的默认样式。要解决此问题,请使.active类更具体:

.generate_div_css.active {
  color: blue;
  border: 1px solid blue;
}

Updated fiddle