在页面刷新javascript后检测div更改

问题描述 投票:-2回答:1

我有一个页面显示带有数字的表格。这些是我的合作伙伴在商店看到的价格。这个页面是专门为他准备的。

价格经常变化。我在WordPress中输入价格。我创建了自定义帖子类型和一个简单的表格来输入这些数字。一个价格=一个自定义字段。字段数约为30。

而且效果很好。但我需要检测价格变化。我希望我的合作伙伴的网站清楚地显示价格变化。

理想的情况是当新价格的DIV颜色发生变化时,旁边会出现一个重置颜色的按钮。这种机制可以让您快速了解价格变化。此页面每10秒刷新一次。

我知道JS的基础知识,但我不知道该怎么做。我怀疑你需要使用SessionStorage。任何人都可以给我指示或粘贴类似解决方案的链接?


我有很多div,例如:

<div id="price1">[types field='price1'][/types]</div>

和JS:

setTimeout(function(){
   window.location.reload(1);
}, 10000);

var price1 = document.getElementById('price1').textContent;

sessionStorage.setItem("price1-box", "price1");

var handle_storage = function () {
        console.log('change in storage! new' + price1);
      };

window.addEventListener("storage", handle_storage, false);
javascript html reload
1个回答
0
投票

我做了解决方案。也许它会对其他人有用。

<div id="eur-k">123</div>


window.onload = function() {

    setTimeout(function(){
       window.location.reload(1);
    }, 30000);

    // define div to add or remove alert
    var eurkDiv = document.getElementById('eur-k');

    // store current content in a variable
    var eurk = eurkDiv.textContent;

    // compare local storage with current content to make alarm
    if (localStorage.getItem('eurkLoc') != eurk) {
        eurkDiv.classList.add("active");
        eurkDiv.innerHTML = eurk + "<button type=\"button\" class=\"potwierdzenie\" onclick=\"resetFunction()\">OK</button>";
    }

    function resetFunction() {
        eurkDiv.classList.remove("active");
        eurkDiv.textContent = eurk;
        localStorage.setItem('eurkLoc', eurk);
    };
};
© www.soinside.com 2019 - 2024. All rights reserved.