从 ajax 中的 rss 提要获取数据。
<p><a href="https://site.ru" target="_blank">test 1</a> <check>47534</check></p>
<p><a href="https://site.ru" target="_blank">test 2</a> <check>4534</check></p>
<p><a href="https://site.ru" target="_blank">test 3</a> <check>45364</check></p>
<p><a href="https://site.ru" target="_blank">test 4</a> <check>4534</check></p>
<p><a href="https://site.ru" target="_blank">test 5</a> <check>7373</check></p>
我需要动态处理它们并在提要数据发生变化的情况下对 ajax 产生影响。
为了了解数据是否发生了变化,我首先将其写入一个 JSON 文件,id 并检查(检查可能会改变)。
[{"id":{"0":"123456"},"check":{"0":"3490"}},{"id":{"0":"678912"},"check":{"0":"1790"}}]
此外,在更新ajax时,您需要检查带有$id的数组以获取存储在json中的$id和$check,如果$check发生了变化,请设置该行的类。如果 id 不在 json 库中,则将其写下来以供以后比较。
ajax.php
$url = "https://site.ru/cached-index.xml";
$content = file_get_contents($url);
$items = new SimpleXmlElement($content);
// запись файла
$json = json_decode(file_get_contents('data.json'), true);
$json[] = [
'id' => $item->id,
'check' => $item->check
];
foreach($items -> channel -> item as $item) {
print '<p><a href="' . $item->link . '" target="_blank">' . $item->title . '</a> <check>' . $item->check . '</check></p>';
}
file_put_contents('data.json', json_encode($json));
$a = полученный check;
$aa = check из json;
$response = ($a != $aa);
if($response === true){
echo 'здесь нужно прописать класс, значение изменилось!';
}else{
echo 'без изменений';
}
页
<div id="ajax"></div>
<script type="text/javascript">
function show()
{
$.ajax({
url: "ajax.php",
cache: false,
success: function(html){
$("#ajax").html(html);
$('.box').first().addClass('animate__animated animate__bounce');
}
});
}
$(document).ready(function(){
show();
setInterval('show()',1000);
});
</script>
如何通过 id 检查检查并在此行发生更改时注册<p class="box">?
像这样的json
[{"id":{"0":"123456"},"check":{"0":"3490"}},{"id":{"0":"678912"},"check":{"0":"1790"}}]
如果数组中的 id 尚未在 json 中,如何保存数据?
有些东西完全混淆了,数据不断地存储在 json 中。如何正确记录和检查数据?
也许js允许你在ajax更新之前保存检查值而无需额外的操作?
据我理解的任务,服务器部分应该简单地返回更新的记录,如果有的话。也就是说,分为三个阶段:
事实证明是这样的:
在js中,接受json并已经输出为链接