由祖父母的父级选择一个css3元素

时间:2017-11-19 17:21:55

标签: javascript html css3 css-selectors

我在我的网站中实现多个rangeliders,因为这是实时生成的代码,我需要通过父母的父类选择一些元素。

这是代码,时间轴类是我自己设置的最后一个,我需要能够编辑.irs-line-right而不更改我的其他滑块

this is the code the timeline class is the last one i can set myself and i need to be able to edit the .irs-line-right without changing my other sliders

1 个答案:

答案 0 :(得分:0)

在您的问题中,您声明要通过父元素访问元素。我不明白为什么这是必要的,但这肯定是可能的。您可以通过js更改元素样式,属性等。但是,如果您只需要使用“irs-line-right”类更改span的样式,最好只使用css。我将在css和javascript中展示如何执行此操作。

CSS示例

在css中你可以改变'irs-line-right'的风格 通过引用'timeline'div(而没有其他id或类),如下所示:

https://jsfiddle.net/2t3w0826/

.timeline > div:nth-of-type(2) > span > span > span > span:nth-of-type(3)
{
  background-color: red;
}

Javascript示例

https://jsfiddle.net/8qceLgw8/

var array_of_all_timelines = document.getElementsByClassName("timeline");
for(var loop=0; loop < array_of_all_timelines.length; loop++)
{
  var element_irs_line_right = array_of_all_timelines[loop].children[1].children[0].children[0].children[0].children[2];
  element_irs_line_right.style.backgroundColor = "red";
};