Javascript 使用JS注入样式时遇到问题

Javascript 使用JS注入样式时遇到问题,javascript,html,css,Javascript,Html,Css,我想向元素添加样式,但没有完整的元素名称。 我使用document.querySelector查找元素并将其id保存到一个名为elID的变量中,这就是我目前所拥有的 var elID = document.querySelector('[id^="slide-bg-"]').id; console.log(elID); document.getElementById(elID).setAttribute("style", "fill-opacit

我想向元素添加样式,但没有完整的元素名称。 我使用document.querySelector查找元素并将其id保存到一个名为elID的变量中,这就是我目前所拥有的

var elID = document.querySelector('[id^="slide-bg-"]').id;
console.log(elID);
document.getElementById(elID).setAttribute("style", "fill-opacity: 0;");
但是,它没有设置属性,而是出现错误“actionator::exeJavaScript-elID.setAttribute不是函数”


首先,
querySelector
的参数应该是字符串
,而不是数组
[]

querySelector
返回第一个匹配的元素而不是ID。因此
elID
将引用
元素而不是ID字符串。另外,
“style”

另外,不要使用
elID.setAttribute(“样式”,“填充不透明度:0;”)转到:

elID.style.fillOpacity = 0;

这似乎可以做到这一点,谢谢大家的帮助。

您需要向querySelector传递一个字符串。您可能缺少一些“”…,querySelector应该已经返回了所需的对象,用getElementById调用包装它不起作用。使用纯香草js?@ivan刚刚用js标记的问题总是纯js问题。很抱歉,这个问题包含太多明显的错误,以至于无法回答:/@Mainstreeem回到书上。
elID.style.fillOpacity = 0;
var elID = document.querySelector('[id^="slide=bg-"]').getAttribute("id");
document.getElementById(elID).setAttribute("style", "fill-opacity: 0;");