如何使用jquery从数组中选择元素并添加类?

时间:2018-05-07 14:43:06

标签: javascript jquery html css arrays

我想根据father-element的命令将“hide”类添加到child-element中 从数组data-node-tag,我已经尝试console.log数组并得到数组,但我不能使用该数组的值来添加类...

P.S。该数组来自名为collapsed_Nodes的cookie,我将其拆分。

我使用js.cookiejquery,这是我的JS Fiddle

$(document).ready(function() {
  var adjustCookie = function() {
    var tags = [];
    $('.accordion_container .hide').each(function() {
      var tag = $(this).prev('.accordion_head').data("node-tag");
      tags.push(tag.replace('#', '#'));
    });

    if (tags.length) {
      Cookies.set('collapsed_Nodes', tags.join('|'), {
        expires: 7
      });
    } else {
      Cookies.remove('collapsed_Nodes');
    }
  }
  
  $(".accordion_head").click(function() {
    var tag = $(this).data("node-tag");
    if ($(this).next(".accordion_body").is(':visible')) {
      $(this).next(".accordion_body").slideUp(300);
      $(this).next(".accordion_body").addClass("hide");
      $(this).children(".plusminus").text('+');
    } else {
      $(this).next(".accordion_body").slideDown(300);
      $(this).next(".accordion_body").removeClass("hide");
      $(this).children(".plusminus").text('-');
    }
    adjustCookie();
  });
  
if (document.cookie.indexOf('collapsed_Nodes') >= 0) {
	var getCollapsed = Cookies.get('collapsed_Nodes');
  var Collapsed = getCollapsed.split('|');
  var arrayLength = Collapsed.length;
  for (var i = 0; i < arrayLength; i++) {
      $("data-node-tag".Collapsed[i]).next(".accordion_body").addClass("hide");
  }
}
});
.accordion_container {
  width: 500px;
}

.accordion_head {
  background-color: skyblue;
  color: white;
  cursor: pointer;
  font-family: arial;
  font-size: 14px;
  margin: 0 0 1px 0;
  padding: 7px 11px;
  font-weight: bold;
}

.accordion_body {
  background: lightgray;
}

.accordion_body p {
  padding: 18px 5px;
  margin: 0px;
}

.plusminus {
  float: right;
}

.hide {
  display: none;
}
<div class="accordion_container">
  <div class="accordion_head" data-node-tag="#1">First Accordian Head<span class="plusminus">-</span>
  </div>
  <div class="accordion_body">
    <p>First Accordian Body, it will have description</p>
  </div>
  <div class="accordion_head" data-node-tag="#2">Second Accordian Head<span class="plusminus">-</span>
  </div>
  <div class="accordion_body">
    <p>Second Accordian Body, it will have description</p>
  </div>
  <div class="accordion_head" data-node-tag="#3">Third Accordian Head<span class="plusminus">-</span>
  </div>
  <div class="accordion_body">
    <p>Third Accordian Body, it will have description</p>
  </div>
</div>

3 个答案:

答案 0 :(得分:2)

另一种方法是通过属性值选择div:

$('[data-node-tag="'+checkCollapsed[i]+'"]').next(".accordion_body").addClass("hide");

JSFiddle:https://jsfiddle.net/yuvakqha/22/

答案 1 :(得分:1)

尝试改变这一点:

$("data-node-tag".Collapsed[i]).next(".accordion_body").addClass("hide");

对此:

$(Collapsed[i]).next(".accordion_body").addClass("hide");

<小时/> Collapsed变量将在.split()

之后输出数组
Array [ "#2", "#3" ]

for循环之后,输出将为:

#2
#3

所以你可以选择jQuery选择器,就像任何ID

一样
$( Collapsed[i] )

答案 2 :(得分:1)

您的选择器错误,类必须位于属性之前,属性应位于括号[]

使用

$("." + Collapsed[i] + [data-node-tag]).next(..).addClass("hide")

https://api.jquery.com/has-attribute-selector/

  

选择具有指定属性且具有任何值的元素。

这是一个简化的例子

&#13;
&#13;
$(".accordion_head[data-node-tag]").addClass("red");
&#13;
.red {
  color: red
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="accordion_container">
  <div class="accordion_head" data-node-tag="#1">First Accordian Head<span class="plusminus">-</span>
  </div>
  <div class="accordion_body">
    <p>First Accordian Body, it will have description</p>
  </div>
  <div class="accordion_head" data-node-tag="#2">Second Accordian Head<span class="plusminus">-</span>
  </div>
  <div class="accordion_body">
    <p>Second Accordian Body, it will have description</p>
  </div>
  <div class="accordion_head" data-node-tag="#3">Third Accordian Head<span class="plusminus">-</span>
  </div>
  <div class="accordion_body">
    <p>Third Accordian Body, it will have description</p>
  </div>
</div>
&#13;
&#13;
&#13;