我目前有几个部门,每个部门都有一个svg。我希望通过jquery获得下一个div的背景颜色(以类为目标),并将其作为前一个父svg的css填充。我试着使用nextAll和prevAll,但是它不起作用。
发布于 2019-09-23 12:06:54
尝试下面的代码
$(document).ready(function(){
$('.awesome').each(function(){
var theColor = $(this).css( "background-color" );
$(this).prev().find('svg').css( "fill", theColor);
});
});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="awesome" style="background-color:white;">
<svg id="curveUpColor" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
<div class="awesome" style="background-color:blue;">
<svg id="curveUpColor" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
<div class="awesome" style="background-color:black;">
<svg id="curveUpColorr" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
<div class="awesome" style="background-color:red;">
<svg id="curveUpColorrrr" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
发布于 2019-09-23 11:49:12
nextAll()方法返回所选元素的所有后续同级元素。
同级元素是共享同一父元素的元素。
我觉得你的div不是同一个家长试试这个:
$(".target_class").parent().parent().find("div")
发布于 2019-09-23 12:06:34
$(document).ready(function() {
//this will get first child of division with awesome classname
var theColor = $("div.awesome:first").css("background-color");
//this will get all the svg(s) except first
$("div.awesome").find("svg:not(:first)").css("background-color", theColor);
});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="awesome" style="background-color:white;">
<svg id="curveUpColor" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
<div class="awesome" style="background-color:blue;">
<svg id="curveUpColor" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
<div class="awesome" style="background-color:black;">
<svg id="curveUpColorr" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
<div class="awesome" style="background-color:red;">
<svg id="curveUpColorrrr" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 C 20 0 50 0 100 100 Z"></path>
</svg>
</div>
https://stackoverflow.com/questions/58055259
复制相似问题