我想在这里创建一个我的里程表使用相同的类并且只声明一次。我试过了document.querySelectorAll('.odometer')
,但它不起作用。请参阅下面的参考
到目前为止我尝试了什么:其他链接(http://jsfiddle.net/9hawxpne/)
const elements = document.querySelectorAll('.odometer');
Array.from(elements).forEach((element, index) => {
ticketOdometer = new Odometer({
el: elements[index] ,
minIntegerLen: 2,
format: 'd',
theme: 'digital',
value: 40
});
});
var odometer_bool_counter = true;
function setRandomOdometerValue() {
if (odometer_bool_counter) {
$('.odometer').html(Math.floor(Math.random() * (40 - 1 + 1) + 1)); // change value
}
}
setInterval(function() {
setRandomOdometerValue();
}, 2500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Odometr includes -->
<link rel="stylesheet" href="http://github.hubspot.com/odometer/themes/odometer-theme-digital.css" />
<script src="http://github.hubspot.com/odometer/odometer.js"></script>
<div id="odometer" class="odometer" style="font-size:72pt;">
40
</div>
<div id="" class="odometer" style="font-size:72pt;">
40
</div>
<div id="" class="odometer" style="font-size:72pt;">
40
</div>
<div id="" class="odometer" style="font-size:72pt;">
40
</div>
<div id="" class="odometer" style="font-size:72pt;">
40
</div>