2

На странице есть несколько кнопок. Внутри кнопок есть div и svg.

<button class="wpOab  " type="button"><div class="QBdTU B5bH7"><svg class="news" color="#8e8e8e" fill="#8e8e8e" height="24"
...
</svg>
</div>
</button>

Классы генерятся случайно, один отличительный момент, что svg внутри кнопок имеет class="news". И нужно через document.querySelector(".myclass"); получить не объект svg, а конкретно его родительский объект button. Как это можно сделать?

1 ответ 1

8

let newsSvg = document.querySelector('.news');

let res = newsSvg.closest('button');

console.log(res);
<button class="wpOab" type="button">
  <div class="QBdTU B5bH7">
    <svg class="news">
    </svg>
  </div>
</button>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.