setAttribute id onresize

问题描述 投票:0回答:0

代码适用于第一个元素,它必须适用于此类的所有元素......

目标是给同一个类的元素分配不同的ID。您需要获取值 data-target="value" 并使用该值分配一个 id。而这一切都与窗口大小有关。

// works only for the first element
const artist = document.querySelector('.catalog__artist');
const getValue = $(artist).attr("data-target");

window.onresize = function() {
  if (window.innerWidth < 900)
  artist.setAttribute('id', getValue);
  else
  artist.removeAttribute('id');
}

我写错了什么?

// does not work for all elements
const artist = document.querySelectorAll('.catalog__artist').forEach(function() {
  const getValue = $(artist).attr("data-target");

  window.onresize = function() {
    if (window.innerWidth < 900)
    artist.setAttribute('id', getValue);
    else
    artist.removeAttribute('id');
  }

});
setattribute onresize
© www.soinside.com 2019 - 2024. All rights reserved.