Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Полифил closest (https://javascript.ru/forum/misc/78617-polifil-closest.html)

Aruta 10.10.2019 05:44

Полифил closest
 
Нашел полифил для closest. Но я тупой наверное:) он не хочет работать.
Пробовал вставлять через ссылку <script src="https://cdn.polyfill.io/v2/polyfill.js?features=Element.prototype.closest"></script>
- тоже не работает.

(function(e){
  e.closest = e.closest || function(css){
    var node = this;

    while (node) {
      if (node.matches(css)) return node;
      else node = node.parentElement;
    }
    return null;
  }
})(Element.prototype);

if (!Element.prototype.matches) {
  Element.prototype.matches =
    Element.prototype.matchesSelector ||
    Element.prototype.mozMatchesSelector ||
    Element.prototype.msMatchesSelector ||
    Element.prototype.oMatchesSelector ||
    Element.prototype.webkitMatchesSelector ||
    function(s) {
      var matches = (this.document || this.ownerDocument).querySelectorAll(s),
        i = matches.length;
      while (--i >= 0 && matches.item(i) !== this) {}
      return i > -1;
    };
}


У меня есть скрипт
let descList = document.querySelectorAll('.upsale__block__bot-right__title-wrap');
	descList.forEach(function(descF) {
	descF.onclick = function viewblock(){  //кнопка/блок, по которой нажимаем
		let listF = this.closest('.upsale__block__sub-right-wrap'); 
		let descshow = listF.children[1]; //выбор блока, который нужно скрыть/показать
		let arrow = this.querySelector('.arrow_up');		
		if (descshow.classList == "dflex"){
			arrow.classList.toggle("");
			listTestF.classList.toggle("dnone");  //удаляем класс
		} else {
			descshow.classList.toggle("dnone");  //удаляем класс
			arrow.classList.toggle("arrow-down");
		}		
	};
});


На сколько я понял, то надо Element поменять на класс блока, который отслеживается, но полифил раньше добавляется и он не видит переменную для нужных блоков.

Подскажите, пожалуйста, почему не хочет работать с моим скриптом указанный выше полифил?:)

рони 10.10.2019 07:37

Aruta,
если вам нужен closest, значит нужен и matches два этих полифила работают в паре, добавить в начало страницы matches , затем closest и ничего не менять в них, потом ваши блоки и только потом ваш скрипт.
не заработает делайте макет.

рони 10.10.2019 07:49

Aruta,
на всякий случай
<script src="https://polyfill.io/v3/polyfill.js?features=Element.prototype.closest%2CElement.prototype.matches"></script>

Aruta 15.10.2019 10:54

Рони, Спасибо за подсказку. Попробую


Часовой пояс GMT +3, время: 11:05.