最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add class to parent in javascript? - Stack Overflow

programmeradmin4浏览0评论

Not jQuery as I would simply do $("this").parent().addClass("active"); but in pure javascript. This is because i don't want to mix up Javascript and jQuery on the following code:

var clickedPath = this.getElement(); 
clickedPath.classList.add("active"); 
var classes = clickedPath.getAttribute('class').match(/\d+/g) || []; 
buttons.forEach(function(btn) { 
    var method = classes.indexOf(btn.getAttribute('data-date')) > -1 ? 'add' : 'remove'; 
    btn.classList[method]('active');
    //btn.parent().addClass("active");
});

UPDATE

This is the HTML starting case before the classes are added:

<ul>
  <li>1500</li>
  <li>1400
      <ul>
        <li>1401</li>
      </ul>
  </li>
</ul>

We have 2 situations:

  • 1500 is single and doesn't have a child
  • 1400 has a child 1401

If I am adding the class active to 1500, it's fine and I can use the code provided in the answer:

btn.parentNode.classList[method]('active');

But If I add the class to 1401, also 1400 should have it, so the followings are the two cases:

<ul>
  <li class="active">1500</li>
  <li>1400
      <ul>
        <li>1401</li>
      </ul>
  </li>
</ul>

Or

<ul>
  <li>1500</li>
  <li class="active">1400
      <ul>
        <li class="active">1401</li>
      </ul>
  </li>
</ul>

Not jQuery as I would simply do $("this").parent().addClass("active"); but in pure javascript. This is because i don't want to mix up Javascript and jQuery on the following code:

var clickedPath = this.getElement(); 
clickedPath.classList.add("active"); 
var classes = clickedPath.getAttribute('class').match(/\d+/g) || []; 
buttons.forEach(function(btn) { 
    var method = classes.indexOf(btn.getAttribute('data-date')) > -1 ? 'add' : 'remove'; 
    btn.classList[method]('active');
    //btn.parent().addClass("active");
});

UPDATE

This is the HTML starting case before the classes are added:

<ul>
  <li>1500</li>
  <li>1400
      <ul>
        <li>1401</li>
      </ul>
  </li>
</ul>

We have 2 situations:

  • 1500 is single and doesn't have a child
  • 1400 has a child 1401

If I am adding the class active to 1500, it's fine and I can use the code provided in the answer:

btn.parentNode.classList[method]('active');

But If I add the class to 1401, also 1400 should have it, so the followings are the two cases:

<ul>
  <li class="active">1500</li>
  <li>1400
      <ul>
        <li>1401</li>
      </ul>
  </li>
</ul>

Or

<ul>
  <li>1500</li>
  <li class="active">1400
      <ul>
        <li class="active">1401</li>
      </ul>
  </li>
</ul>
Share Improve this question edited Nov 7, 2017 at 5:20 tonys 3,98435 silver badges40 bronze badges asked Nov 19, 2016 at 22:17 rob.mrob.m 10.6k21 gold badges85 silver badges174 bronze badges 1
  • you would be looking for btn.parentNode instead of btn.parent() if I am reading your code right. – Jhecht Commented Nov 19, 2016 at 22:19
Add a comment  | 

3 Answers 3

Reset to default 30

You mention " i don't want to mix up js and jQuery on the following code" but you are actually mixing vanilla DOM APIs with jQuery methods. .parent and .addClass are jQuery functions. You can code:

btn.parentNode.classList.add("active");

Also consider parentElement as an alternative to parentNode:

btn.parentElement.classList.add("active");

You need .parentNode on the element.

Something like this.

var clickedPath = this.getElement();
clickedPath.classList.add("active");
var classes = clickedPath.getAttribute('class').match(/\d+/g) || [];
buttons.forEach(function(btn) {
  var method = classes.indexOf(btn.getAttribute('data-date')) > -1 ? 'add' : 'remove';
  btn.classList[method]('active');
  btn.parentNode.classList.add("active");
});
发布评论

评论列表(0)

  1. 暂无评论