Javascript 在循环中使用.getAttribute添加样式

Javascript 在循环中使用.getAttribute添加样式,javascript,loops,getattribute,Javascript,Loops,Getattribute,第一个帖子就在这里。希望有人能帮忙 我试图将样式添加到我创建的视差旋转木马中,方法是在HTML中添加数据属性,然后使用JS在旋转木马中遍历每个图像。我可以将“数据背景”添加到内联样式中,但“数据位置”不能 预期产量 <div class="parallax-container parallax-scale" data-background="//localhost:3000/wp-content/uploads/2020/03/animage.jpg&quo

第一个帖子就在这里。希望有人能帮忙

我试图将样式添加到我创建的视差旋转木马中,方法是在HTML中添加数据属性,然后使用JS在旋转木马中遍历每个图像。我可以将“数据背景”添加到内联样式中,但“数据位置”不能

预期产量

<div class="parallax-container parallax-scale" 
data-background="//localhost:3000/wp-content/uploads/2020/03/animage.jpg" 
data-position="50% 50%" data-swiper-parallax="1130.25" 
style="transform: translate3d(0px, 0px, 0px); background-image: url("//localhost:3000/wp-content/uploads/2020/03/animage.jpg"); background-position: 50% 50%;">

var list=document.getElementsByClassName('parallax-container');
对于(变量i=0;i

我做了一个片段。请删除PHP并仅发布干净的HTML和脚本您希望
data position=“50%50%”
如何变成
data position=“center”
?您可以使用查看样式是否无效(删除)。由于您使用的是data-*属性,因此也可以使用。脚本看起来有效。感谢您的快速响应。我修改了mplungjan的代码片段,以反映预期的输出应该是什么,以及实际发生了什么。很抱歉,数据位置属性中的输入错误。背景位置内联样式应该输出任何输入到数据位置属性的内容。谢谢Ryuno Ki。已尝试使用dataset,但控制台中没有错误。。。