gpt4 book ai didi

javascript - 如果票数 > 0,XML 仅显示

转载 作者:行者123 更新时间:2023-12-03 05:41:50 26 4
gpt4 key购买 nike

我试图显示<Candidate></Candidate>位于拉特兰县,只有得票高于 0。现在,它仅显示拉特兰县的所有投票。

https://jsfiddle.net/jeffd/yqbwaxts/3/

https://99centbeats.com/ResultsData.xml

到目前为止:

   $(document).ready(function() {
$.ajax({
type: 'GET',
url: 'https://99centbeats.com/ResultsData.xml',
//url: 'https://vtelectionresults.sec.state.vt.us/rss/2713/ResultsData.xml',
crossDomain: true,
success: parseXml
});
});
function parseXml(xml) {
$(xml).find('Town').each(function() {
var county = $(this).find('County').text(); //change to county for 'County'
window.votes = $(this).find('Votes').text();
if (county == 'RUTLAND') { // change to 'RUTLAND' for Rutland County


if (votes !== '0'){


window.townname = $(this).find('TownName').text();
window.officename = $(this).find('OfficeName, Name, Votes');
var newItems = $.map(officename, function(i) {
$(i).filter('0')
return $(i).text();
});
newItems2 = ("" + newItems).replace(/,/g, " - ");
$(".marquee").append(' - <strong>' + townname + '</strong>' + ' - ' + newItems2);
}
}
});
$('.marquee').marquee({
duration: 5000 // Speed of the counter (Lower = Faster)
});
}
setTimeout(function() {
window.location.reload(1);
}, 300000); // Refresh Results Every 5 Minutes

最佳答案

看起来你的处理方式相当随意,只是抓取元素并将它们串在一起。最好有条不紊地找到城镇内的办公室,然后是他们的候选人,然后通过投票等方式过滤这些候选人。

以下内容应该有效,尽管目前拉特兰的一个城镇似乎只有一名候选人拥有选票:

//Must have Allow-Control-Allow-Origin chrome plugin installed to work

$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'https://99centbeats.com/ResultsData.xml',
//url: 'https://vtelectionresults.sec.state.vt.us/rss/2713/ResultsData.xml',
crossDomain: true,
success: parseXml
});
});

function textForOffice(office) {
var candidates = $(office).find('Candidate').filter(function(i, el) {
return $(el).find('Votes').text() > 0;
});

if (candidates.length === 0) {
return '';
}

var officeName = $(office).find('OfficeName').text();

var candidateValues = candidates.toArray().map(function (el) {
return $(el).find('Name').text() + ' - ' + $(el).find('Votes').text();
});

return ' - ' + officeName + ' - ' + candidateValues.join(' - ');
}

function parseXml(xml) {
$(xml).find('Town').each(function() {
var town = $(this);
var county = town.find('County').text(); //change to county for 'County'

if (county == 'RUTLAND') { // change to 'RUTLAND' for Rutland County
var townname = town.find('TownName').text();

var offices = town.find('Office');

var textForOffices = offices.toArray().map(textForOffice);

$(".marquee").append(' - <strong>' + townname + '</strong>' + textForOffices.join(''));
}
});
$('.marquee').marquee({
duration: 5000 // Speed of the counter (Lower = Faster)
});
}
setTimeout(function() {
window.location.reload(1);
}, 300000); // Refresh Results Every 5 Minutes
.rssfeed {
background-color: black;
color: white;
bottom: 0;
font-family: Gotham, Helvetica Neue, Helvetica, Arial, " sans-serif";
height: 150px;
line-height: 150px;
font-size: 32px;
}

.marquee {
overflow: hidden;
width: 100%;
white-space: nowrap;
}

.description {
width: 100%;
height: 50px;
background: red;
color: white;
line-height: 50px;
text-align: center;
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 24px;
}
<div class="description">Rutland City - Vote Count Per Ward</div>
<div class="rssfeed">
<div class="marquee"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.marquee/1.4.0/jquery.marquee.min.js"></script>

关于javascript - 如果票数 > 0,XML 仅显示 <Candidate></Candidate>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40492629/

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