代码适用于第一个元素,它必须适用于此类的所有元素......
目标是给同一个类的元素分配不同的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方法的attributeValue参数
• 清除html中表达式${expression}存储的值?
• 什么可能导致自定义古腾堡块的“setAttribute”功能无法与保存/发布按钮一起使用?
• 我的 PHP 代码工作正常,但是当我通过 instagram 或 facebook 上的链接打开它时,上面写着,“名称”列不能为空
• PHP OOP 数据库连接使用带有类和函数的单独配置文件
• 如何比较4列
• 使用Gradle的Sonarqube 8的Jacoco代码覆盖率。
• 如何从应用程序(客户端)ID 和目录(租户)ID 获取对象 ID?