我想更改价格低于500英镑的轮播元素的背景颜色。我需要在JavaScript DOM中执行此操作,但似乎无济于事。...这是到目前为止我想到的内容:
// Get the elements- price and each carousel element
let price = document.getElementsByClassName("price");
let greenBackground = document.getElementsByClassName("close")[0];
//Prices are given as strings so I tried to convert it to a number to be able to compare
let priceAsNumber = parseInt(price, 10);
//and finally I tried to apply the condition to change the background color
function changeBackground () {
if (priceAsNumber <= 500) {
document.greenBackground.style.background = #81CA81;
}
};
changeBackground();
我是一个初学者,所以代码可能会丢失很多,但是我非常感谢一些实现结果的技巧!
这里是html的示例:
<div class="owl-stage-outer"><div class="owl-stage" style="transform: translate3d(0px, 0px, 0px); transition: all 0s ease 0s; width: 2446px;"><div class="owl-item active" style="width: 152.857px;"><li class="all-months close selected"><label class="date-price-holidays">All Holidays<input type="radio" id="filter60_24" name="filter60" value="24" data-filter="24" data-compare="== 24 && r.matchFlights[jvh.search.settings.currentMonth] >= jvh.search.settings.currentAirportId && '24' == " data-no="50" data-type="groupTxt" data-pos="-1" class="noautostyle filter-active" data-name-value="month" checked="checked" data-no-remember-notification="true"><span><span class="count">516</span> <span>found</span></span></label></li></div><div class="owl-item active" style="width: 152.857px;"><li class="close">
<label class="date-price-holidays">
<span class="month">Mar<span class="year">2020</span></span>
<input type="radio" id="filter60_2" name="filter60" value="2" data-filter=" r.matchFlights[jvh.search.settings.currentMonth] >= jvh.search.settings.currentAirportId && r.sale[jvh.search.settings.currentMonth]" data-compare="> 0" data-no="50" data-type="groupTxt" data-pos="2" class="noautostyle" data-name-value="month" data-no-remember-notification="true">
<span class="count-and-price"><span class="count">2</span> <span class="holidays">holidays</span><span class="from"> from </span><span class="price">£629<span class="pp">pp</span></span></span>
</label>
</li></div><div class="owl-item active" style="width: 152.857px;"><li class="close">
<label class="date-price-holidays">
<span class="month">Apr<span class="year">2020</span></span>
<input type="radio" id="filter60_3" name="filter60" value="3" data-filter=" r.matchFlights[jvh.search.settings.currentMonth] >= jvh.search.settings.currentAirportId && r.sale[jvh.search.settings.currentMonth]" data-compare="> 0" data-no="50" data-type="groupTxt" data-pos="3" class="noautostyle" data-name-value="month" data-no-remember-notification="true">
<span class="count-and-price"><span class="count">105</span> <span class="holidays">holidays</span><span class="from"> from </span><span class="price">£249<span class="pp">pp</span></span></span>
</label>
</li></div><div class="owl-item active" style="width: 152.857px;"><li class="close">
<label class="date-price-holidays">
<span class="month">May<span class="year">2020</span></span>
<input type="radio" id="filter60_4" name="filter60" value="4" data-filter=" r.matchFlights[jvh.search.settings.currentMonth] >= jvh.search.settings.currentAirportId && r.sale[jvh.search.settings.currentMonth]" data-compare="> 0" data-no="50" data-type="groupTxt" data-pos="4" class="noautostyle" data-name-value="month" data-no-remember-notification="true">
<span class="count-and-price"><span class="count">450</span> <span class="holidays">holidays</span><span class="from"> from </span><span class="price">£289<span class="pp">pp</span></span></span>
</label>
</li></div><div class="owl-item active" style="width: 152.857px;"><li class="close">
<label class="date-price-holidays">
<span class="month">Jun<span class="year">2020</span></span>
<input type="radio" id="filter60_5" name="filter60" value="5" data-filter=" r.matchFlights[jvh.search.settings.currentMonth] >= jvh.search.settings.currentAirportId && r.sale[jvh.search.settings.currentMonth]" data-compare="> 0" data-no="50" data-type="groupTxt" data-pos="5" class="noautostyle" data-name-value="month" data-no-remember-notification="true">
<span class="count-and-price"><span class="count">402</span> <span class="holidays">holidays</span><span class="from"> from </span><span class="price">£559<span class="pp">pp</span></span></span>
</label>
[使用let price = document.getElementsByClassName("price");
时,您将收到一个HTMLCollection对象。
我的建议:如果只想使用querySelector
仅获得一个元素如下所示:
let price = document.querySelector('.price');
let greenBackground = document.getElementsByClassName('.close');
所以您在这里收到DOM元素对象。然后,您应该首先获取它的值,然后再对其进行解析。
let priceAsNumber = parseInt(price.innerText, 10);
因此,整个解决方案应类似于下一个:
let price = document.querySelector('.price');
let greenBackground = document.getElementsByClassName('.close');
let priceAsNumber = parseInt(price.innerText, 10);
function changeBackground () {
if (priceAsNumber <= 500) {
document.greenBackground.style.background = #81CA81;
}
};
changeBackground();
这里是工作代码的快速示例,以及关于punker的示例
<script>
function changeColor(){
let priceNodes = document.getElementsByClassName('price');
for(let i = 0; i< priceNodes.length; i++){
//get value and remove euro sign
let nodeWithPrice = priceNodes[i].textContent.slice(1);
console.log(nodeWithPrice);
//convert value to numeric
nodeWithPrice = parseInt(nodeWithPrice);
//check according your condition
if(nodeWithPrice <= 500){
//if price less or equal of 500 - change color of html-node
//find your node using Element.closest() method
console.log(priceNodes[i].closest('.close'));
priceNodes[i].closest('.close').style.background = '#81CA81';
}
}
}
changeColor();
</script>