gpt4 book ai didi

javascript - 使用 JavaScript,如何使用 getElementByClassName 获取另一个元素内具有特定 className 的元素?

转载 作者:行者123 更新时间:2023-11-28 06:02:06 25 4
gpt4 key购买 nike

您好,stackoverflow 专家,我遇到了这个问题,请在这里帮助我。

我有这个代码; HTML 是软件生成的,所以它不是很有效,我知道,我们不必担心这一点。我想完全用 JavaScript 编写 jQuery 部分,我不想使用 jQuery,只是简单的 JavaScript。我该怎么做?

$(".skillbar-container").each(function() {

var x = $(this).find(".skillbar-percent").html();
x = x.replace(/\s+/g, '');

$(this).find(".skillbar-bar").width(x + "%");
});
.skillbar-container {
margin-bottom: 24px;
}
.skillbar-texts {
margin-bottom: 6px;
font-size: 15px;
}
.skillbar-title {
display: inline-block;
float: left;
text-transform: capitalize;
}
.percent-mark {
display: inline;
float: right;
}
.skillbar-percent {
display: inline-block;
float: right;
}
.skillbar {
height: 3px;
border-top-right-radius: 2px;
border-bottom-right-radius: 2px;
background-color: red;
overflow: hidden;
}
.skillbar-bar {
width: 63%;
height: 100%;
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="skillbars">

<div class="skillbar-container team-skillbar">
<div class="skillbar-texts">

<div class="skillbar-title">
Sales Management
</div>

<div class="percent-mark">
%
</div>

<div class="skillbar-percent wf-affected">
76
</div>

</div>

<div class="skillbar">
<div class="skillbar-bar">
</div>
</div>

</div>

<div class="skillbar-container team-skillbar">
<div class="skillbar-texts">

<div class="skillbar-title">
Sales Management
</div>

<div class="percent-mark">
%
</div>

<div class="skillbar-percent wf-affected">
15
</div>

</div>
<div class="skillbar">
<div class="skillbar-bar">
</div>
</div>
</div>
</div>

这是我的尝试:

function myFunction() {
var x = document.getElementsByClassName("skillbar-container");
var i;
for (i = 0; i < x.length; i++) {
var y = x[i].getElementsByClassName("skillbar-percent").innerHTML;
var z = x[i].getElementsByClassName("skillbar-bar");
z.style.width = y + '%';
}
}
myFunction();

但它不起作用,显然这个: document.getElementsByClassName("skillbar-container")getElementsByClassName("skillbar-bar"); 不起作用,querySelector 也不支持.

那我该怎么办?

最佳答案

  1. 将 2 个类收集到 NodeList 中
  2. 将 2 个 NodeList 转换为数组。
  3. 迭代 2 个 for 循环(一个嵌套在另一个循环中)
  4. 在第一个循环中,提取了百分比中的文本(不需要正则表达式)
  5. 确保它是一个带有 parseInt 的数字
  6. 将该数字存储在数组中 stats[]
  7. 在第二个循环中,使用 stats[j] 的相应索引作为相应条形的新宽度。
  8. 添加了一些更多的栏,因为它很有趣。 :P

片段

var skBar = document.getElementsByClassName('skillbar-bar');
var skPer = document.getElementsByClassName('skillbar-percent');
var barArr = Array.prototype.slice.call(skBar);
var perArr = Array.prototype.slice.call(skPer);
var i;
var j;
var stats = [];

for (i = 0; i < perArr.length; i++) {
var x = perArr[i].textContent;
x = parseInt(x, 10);
stats.push(x);
console.log('percent: '+x);

for (j = 0; j < barArr.length; j++) {
var y = stats[j];
barArr[j].style.width = y + "%";

}
}
.skillbar-container {
margin-bottom: 24px;
}
.skillbar-texts {
margin-bottom: 6px;
font-size: 15px;
}
.skillbar-title {
display: inline-block;
float: left;
text-transform: capitalize;
}
.percent-mark {
display: inline;
float: right;
}
.skillbar-percent {
display: inline-block;
float: right;
}
.skillbar {
height: 3px;
border-top-right-radius: 2px;
border-bottom-right-radius: 2px;
background-color: red;
overflow: hidden;
}
.skillbar-bar {
height: 100%;
background-color: blue;
}
<div class="skillbars">

<div class="skillbar-container team-skillbar">
<div class="skillbar-texts">

<div class="skillbar-title">
Sales Management
</div>

<div class="percent-mark">
%
</div>

<div class="skillbar-percent wf-affected">
76
</div>

</div>

<div class="skillbar">
<div class="skillbar-bar">
</div>
</div>

</div>

<div class="skillbar-container team-skillbar">
<div class="skillbar-texts">

<div class="skillbar-title">
Sales Management
</div>

<div class="percent-mark">
%
</div>

<div class="skillbar-percent wf-affected">
15
</div>

</div>
<div class="skillbar">
<div class="skillbar-bar">
</div>
</div>
</div>

<div class="skillbar-container team-skillbar">
<div class="skillbar-texts">

<div class="skillbar-title">
Sales Management
</div>

<div class="percent-mark">
%
</div>

<div class="skillbar-percent wf-affected">
92
</div>

</div>

<div class="skillbar">
<div class="skillbar-bar">
</div>
</div>



</div>

<div class="skillbar-container team-skillbar">
<div class="skillbar-texts">

<div class="skillbar-title">
Sales Management
</div>

<div class="percent-mark">
%
</div>

<div class="skillbar-percent wf-affected">
41
</div>

</div>

<div class="skillbar">
<div class="skillbar-bar">
</div>
</div>

</div>
<div class="skillbar-container team-skillbar">
<div class="skillbar-texts">

<div class="skillbar-title">
Sales Management
</div>

<div class="percent-mark">
%
</div>

<div class="skillbar-percent wf-affected">
50
</div>

</div>

<div class="skillbar">
<div class="skillbar-bar">
</div>
</div>

</div>

</div>

关于javascript - 使用 JavaScript,如何使用 getElementByClassName 获取另一个元素内具有特定 className 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37198856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com