jquery - 当有多个要填充的元素时,代码不起作用
问题描述
该代码完美运行,但仅适用于一个元素,如下所示它是根据百分比填充的。
$(function () {
$("div.percent").each(function(){
var s = $("span.pct").text() + ' 100%';
$(this).css({"background-size": s})
console.log(s);
});
});
.avg-percent {
margin-bottom: 0.2em;
margin-top: 0.2em;
}
.flex {
display: flex;
}
.relative {
position: relative;
}
.very-satisfied {
background-image: linear-gradient(to right, #57bb8a, #57bb8a);
}
.percent {
width: 90%;
background-size: 1% 100%;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: space-between;
}
.percent span:nth-of-type(1) {
display: none;
}
span.avg-vote {
flex: 1;
padding-left: 1em;
font-size: 0.8em;
align-items: center;
display: flex;
vertical-align: middle;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="avg-percent div flex relative">
<div class="percent very-satisfied">
<span class="pct">70%</span>
<span class="avg-vote">0</span>
</div>
</div>
问题来了,当您要填充更多元素时,代码将停止工作。
$(function () {
$("div.percent").each(function(){
var s = $("span.pct").text() + ' 100%';
$(this).css({"background-size": s})
console.log(s);
});
});
.avg-percent {
margin-bottom: 0.2em;
margin-top: 0.2em;
}
.flex {
display: flex;
}
.relative {
position: relative;
}
.very-satisfied {
background-image: linear-gradient(to right, #57bb8a, #57bb8a);
}
.percent {
width: 90%;
background-size: 1% 100%;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: space-between;
}
.percent span:nth-of-type(1) {
display: none;
}
span.avg-vote {
flex: 1;
padding-left: 1em;
font-size: 0.8em;
align-items: center;
display: flex;
vertical-align: middle;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="avg-percent div flex relative">
<div class="percent very-satisfied">
<span class="pct">60%</span>
<span class="avg-vote">0</span>
</div>
</div>
<div class="avg-percent div flex relative">
<div class="percent very-satisfied">
<span class="pct">40%</span>
<span class="avg-vote">0</span>
</div>
</div>
在仅评估单个元素的第一个示例中,在控制台中它向我们展示了这一点70% 100%
,但是当我想在控制台中使用 multipli 时,它向我展示了这一点60%40% 100%
60%40% 100%
那么如何让代码适用于多个背景填充?
解决方案
您需要针对span.pct
目标元素中的特定内容。
$(function () {
$("div.percent").each(function(){
var s = $(this).find("span.pct").text() + ' 100%'; // Change this line
$(this).css({"background-size": s})
console.log(s);
});
});
.avg-percent {
margin-bottom: 0.2em;
margin-top: 0.2em;
}
.flex {
display: flex;
}
.relative {
position: relative;
}
.very-satisfied {
background-image: linear-gradient(to right, #57bb8a, #57bb8a);
}
.percent {
width: 90%;
background-size: 1% 100%;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: space-between;
}
.percent span:nth-of-type(1) {
display: none;
}
span.avg-vote {
flex: 1;
padding-left: 1em;
font-size: 0.8em;
align-items: center;
display: flex;
vertical-align: middle;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div class="avg-percent div flex relative">
<div class="percent very-satisfied">
<span class="pct">60%</span>
<span class="avg-vote">0</span>
</div>
</div>
<div class="avg-percent div flex relative">
<div class="percent very-satisfied">
<span class="pct">40%</span>
<span class="avg-vote">0</span>
</div>
</div>
推荐阅读
- python - 附加 true 或 false 的函数无法正常工作
- laravel - 安装letsencrypt后如何修复路由器上的404 nginx错误
- python - 有没有更好的方法在 pygame 中创建瓷砖地图?
- javascript - Firestore 设置错误:“timestampsInSnapshots 设置现在默认为 true”
- flutter - 小部件未在 android 中显示但在 ios 中显示
- java - org.springframework.beans.factory.annotation.AnnotatedBeanDefinition.setRole(I)V 当@EnableKafka
- xml - 从 bash 中的 xml 文件中删除特定标签
- python - 我收到分配前局部变量 cruty 引用的错误
- php - 自动化 PHP 数组中的标签数量
- amazon-web-services - EC2 体验非常高的带宽