首页 > 解决方案 > 如何创建添加角色 =“演示文稿”的脚本

问题描述

我想创建role="presentation"动态添加到页面上所有图像的脚本,但我不知道为此使用什么类型的函数。图像是动态添加的,所以我不能手动添加。下面我上传我的源代码。

//.attachment-large(class that img has)
//.elementor-inner-section (class that the div with images has)

(()=>{ function AddRole() { 
    const imageHook = document.querySelectorAll(".attachment-large"); 
    const boxHook = document.querySelector(".elementor-inner-section"); 
    boxHook.forEach((box) => { 
        imageHook.setAttribute("style","role: presentation") }); 
} AddRole() })();

标签: javascriptattributes

解决方案


role是一个属性本身(以及style)。所以你需要运行:

imageHook.setAttribute('role', 'presentation'); 

顺便说一句,运行它imageHook必须足够,没有循环遍历boxHook- 你似乎对此没有依赖性。


推荐阅读